标签:tom source desc rms amp return code while ons
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8367 Accepted Submission(s): 3139
规律:数串的右边一般是上一个数串,数串的左半边是上两个数串+上一个数串的左半区
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<algorithm> 5 #include<iostream> 6 using namespace std; 7 #define ll long long 8 const int N = 1010; 9 struct Node{ 10 string sum; 11 string leftsum; 12 int left,right;//zuo ban qu 13 }node[N]; 14 15 string add(string s1,string s2){ //大数 s1 + s2 16 if(s1.length()<s2.length()){ 17 string temp=s1; 18 s1=s2; 19 s2=temp; 20 } 21 for(int i=s1.length()-1,j=s2.length ()-1;i>=0;i--,j--){ 22 s1[i]=char(s1[i]+( j>=0 ? s2[j]-‘0‘ : 0)); 23 if(s1[i]-‘0‘>=10) { 24 s1[i]=char( (s1[i]-‘0‘)%10+‘0‘ ); 25 if(i) s1[i-1]++; 26 else s1="1"+s1; 27 } 28 } 29 return s1; 30 } 31 32 void init() 33 { 34 /* for(int i = 0; i < N; i++){ 35 node[i].sum ="0"; 36 node[i].leftsum = "0"; 37 }*/ 38 node[1].sum = "0", node[1].left = 0, node[1].right = 0, node[1].leftsum = "0"; 39 node[2].sum = "1", node[2].left = 1, node[2].right = 0, node[2].leftsum = "0"; 40 for(int i = 3; i < N; i++) 41 { 42 node[i].leftsum = add(node[i-2].sum,node[i-1].leftsum); 43 node[i].left = node[i-2].left; 44 node[i].right = node[i-1].right; 45 node[i].sum=add(node[i-1].sum,node[i].leftsum); 46 if(node[i].right==node[i-1].left&&node[i].right == 0) node[i].sum = add(node[i].sum,"1"); 47 } 48 } 49 int main() 50 { 51 int n; 52 init(); 53 while(~scanf("%d",&n)){ 54 // printf("%s\n",node[n].sum); 55 cout<<node[n].sum<<endl; 56 } 57 return 0; 58 }
大数加法模板
1 string add(string s1,string s2){ //大数 s1 + s2 2 if(s1.length()<s2.length()){ 3 string temp=s1; 4 s1=s2; 5 s2=temp; 6 } 7 for(int i=s1.length()-1,j=s2.length ()-1;i>=0;i--,j--){ 8 s1[i]=char(s1[i]+( j>=0 ? s2[j]-‘0‘ : 0)); 9 if(s1[i]-‘0‘>=10) { 10 s1[i]=char( (s1[i]-‘0‘)%10+‘0‘ ); 11 if(i) s1[i-1]++; 12 else s1="1"+s1; 13 } 14 } 15 return s1; 16 }
hdu_1041(Computer Transformation) 大数加法模板+找规律
标签:tom source desc rms amp return code while ons
原文地址:http://www.cnblogs.com/shanyr/p/7355911.html