标签:斐波那契 java clu 问题 line jin you mission font
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14776 Accepted Submission(s): 4923
100
4203968145672990846840663646 Note: No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.
就是一个变种的斐波那契,在处理大数相加的时候我用了string 被bug绊了好久...明天好好学学string
#include<bits/stdc++.h> using namespace std; int n,i; string bigadd(string a,string b) { int jin=0,i; char ai,bi; string anss=a; int lena=a.size(); int lenb=b.size(); int lenmax=max(lena,lenb); int p=lena-1; int q=lenb-1; for(i=lenmax-1;i>=0;i--) { if(p<0) ai=‘0‘; else ai=a[p]; if(q<0) bi=‘0‘; else bi=b[q]; anss[i]=((ai-‘0‘+bi-‘0‘+jin)%10)+‘0‘; jin=(ai-‘0‘+bi-‘0‘+jin)/10; p--; q--; } if(jin) { char x=jin+‘0‘; anss=x+anss; } return anss; } /* int main() { while(scanf("%d",&n)!=EOF) { string a="1"; string b="1"; string c="1"; string d="1"; for(i=5;i<=n;i++) { string temp=d; d=bigadd(bigadd(a,b),bigadd(c,d)); a=b; b=c; c=temp; } cout<<a <<b <<c <<d<<endl; } } */ int main(){ string a[8008]; //我之前用了4个string 出错了 好像是和长度有关 a[1]="1"; a[2]="1"; a[3]="1"; a[4]="1"; for(i=5;i<8008;++i) a[i]=bigadd(bigadd(bigadd(a[i-1],a[i-2]),a[i-3]),a[i-4]); while(scanf("%d",&n)!=EOF) { cout<<a[n]<<endl; } return 0; }
HDU 1250 Hat's Fibonacci (递推、大数加法、string)
标签:斐波那契 java clu 问题 line jin you mission font
原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/11330299.html