大数加法 + 打表。
代码:
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 6 using namespace std; 7 8 struct node 9 { 10 char s[1000]; 11 }; 12 13 int main() 14 { 15 struct node f[1001]; 16 int i, j, l, n, m; 17 int a[1000], b[1000], r[1000]; 18 int alen, blen, len, k; 19 memset(f[1].s, 0, sizeof(f[1].s)); 20 memset(f[2].s, 0, sizeof(f[2].s)); 21 strcpy(f[1].s, "1"); 22 strcpy(f[2].s, "1"); 23 for (i=3; i<1001; i++) 24 memset(f[i].s, 0, sizeof(f[i].s)); 25 for (i=3; i<1001; i++) 26 { 27 memset(a, 0, sizeof(a)); 28 memset(b, 0, sizeof(b)); 29 memset(r, 0, sizeof(r)); 30 memset(f[i].s, 0, sizeof(f[i].s)); 31 alen=strlen(f[i-1].s); 32 blen=strlen(f[i-2].s); 33 for (j=0; j<alen; j++) 34 a[j]=f[i-1].s[j]-'0'; 35 for (j=0; j<blen; j++) 36 b[j]=f[i-2].s[j]-'0'; 37 len=alen>blen?alen:blen; 38 k=0; 39 for (j=0; j<len; j++) 40 { 41 r[k]+=a[k]+b[k]; 42 if (r[k] >=10) 43 { 44 r[k]%=10; 45 r[k+1]++; 46 } 47 k++; 48 for (l=k; r[l] >= 10; l++) 49 { 50 r[l]%=10; 51 r[l+1]++; 52 } 53 } 54 for (j=0; j<=k; j++) 55 f[i].s[j]=r[j]+'0'; 56 } 57 int flag; 58 cin >> n; 59 while(n--) 60 { 61 cin >> m; 62 flag=0; 63 for (i=999; i>=0; i--) 64 { 65 if ((f[m].s[i]!='\0' && f[m].s[i] != '0') || flag) 66 { 67 flag=1; 68 printf("%c", f[m].s[i]); 69 } 70 } 71 cout << endl; 72 } 73 return 0; 74 }