acm.hdu.edu.cn/showproblem.php?pid=1250
周六晚的BestCoder(有米!) |
Hat's FibonacciTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9277 Accepted Submission(s): 3029
Problem Description
A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take a number as input, and print that Fibonacci number.
Input
Each line will contain an integers. Process to end of file.
Output
For each case, output the result in a line.
Sample Input
Sample Output
|
#include <iostream> #include <string.h> using namespace std; char s[9999][2015]; void fan(char str[]) { int l=strlen(str); char s; for(int i=0;i<l/2;i++) { s=str[i]; str[i]=str[l-i-1]; str[l-i-1]=s; } } int min(int a,int b) { if(a>b) return b; else return a; } void add(char s[],char s1[],char s2[],char s3[],char s4[]) { int l1=strlen(s1); int l3=strlen(s3); int l2=strlen(s2); int l4=strlen(s4); fan(s1); fan(s2); fan(s3); fan(s4); int q=0,temp; int i; for(i=0;i<l1;i++) { temp=s1[i]-'0'+s2[i]-'0'+q+s3[i]-'0'+s4[i]-'0'; s[i]=temp%10+'0'; q=temp/10; } for(i=l1;i<l2;i++) { temp=s2[i]-'0'+q+s3[i]-'0'+s4[i]-'0'; s[i]=temp%10+'0'; q=temp/10; } for(i=l2;i<l3;i++) { temp=q+s3[i]-'0'+s4[i]-'0'; s[i]=temp%10+'0'; q=temp/10; } for(i=l3;i<l4;i++) { temp=q+s4[i]-'0'; s[i]=temp%10+'0'; q=temp/10; } // int j; /* if(l1<l2) { for(j=l1;j<l2;j++) { temp=s2[j]-'0'+q; s[j]=temp%10+'0'; q=temp/10; } } else { for(j=l2;j<l1;j++) { temp=s1[j]-'0'+q; s[j]=temp%10+'0'; q=temp/10; } }*/ if(q) { s[i++]=q+'0'; } s[i++]='\0'; fan(s); fan(s1); fan(s2); fan(s3); fan(s4); } void get_fib() { s[1][0]='1'; s[1][1]='\0'; s[2][0]='1'; s[2][1]='\0'; s[3][0]='1'; s[3][1]='\0'; s[4][0]='1'; s[4][1]='\0'; char str[2015],str2[2015]; for(int i=5;i<=7050;i++) { // add(str,s[i-3],s[i-4]); // add(str2,s[i-2],s[i-1]); add(s[i],s[i-4],s[i-3],s[i-2],s[i-1]); // memset(str,0,sizeof(str)); // memset(str2,0,sizeof(str2)); } //cout<<strlen(s[7050])<<endl; // cout<<s[100]<<endl; // cout<<strlen(s[999])<<endl; // cout<<s[1]<<s[2]<<s[3]<<s[4]<<endl; // cout<<s[5]<<endl; /* int d[150]; memset(d,0,sizeof(d)); for(int i=1;i<=500;i++) d[strlen(s[i])]++; for(int i=1;i<=100;i++) if(d[i]==0) cout<<i<<endl; //cout<<strlen(s[i])<<endl;*/ } /*bool find(char str[]) { for(int i=1;i<505;i++) { if(strcmp(s[i],str)==0) return true; } return false; } int find_(char str[]) { for(int i=504;i>=1;i--) { if(strlen(s[i])==strlen(str)&&strcmp(s[i],str)<=0) { return i; } if(strlen(s[i])<strlen(str)) break; } for(int i=504;i>=1;i--) if(strlen(s[i])<strlen(str)) return i; }*/ int main() { int n; get_fib(); while(cin>>n) { cout<<s[n]<<endl; } /*if(strcmp(a,"0")==0&&strcmp(b,"0")==0) break; if(!find(a)&&!find(b)) { cout<<find_(b)-find_(a)<<endl; //cout<<"a"<<endl; } else if(find(b)&&!find(a)) { cout<<find_(b)-find_(a)<<endl; // cout<<"c"<<endl; } else if(find(a)&&find(b)) { cout<<find_(b)-find_(a)+1<<endl; // cout<<"b"<<endl; } else { cout<<find_(b)-find_(a)+1<<endl; // cout<<"d"<<endl; } } //ut << "Hello world!" << endl;*/ return 0; }