Hat's Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5270 Accepted Submission(s): 1764
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
100
Sample Output
4203968145672990846840663646
Note:
No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.
Author
戴帽子的
Recommend
Ignatius.L
1 #include<cstdio> 2 #include<cmath> 3 #include<string> 4 #include<iostream> 5 using namespace std; 6 7 string sum(string s1,string s2) 8 { 9 if(s1.length()<s2.length()) 10 { 11 string temp=s1; 12 s1=s2; 13 s2=temp; 14 } 15 for(int i=s1.length()-1,j=s2.length ()-1;i>=0;i--,j--) 16 { 17 s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0)); 18 if(s1[i]-'0'>=10) 19 { 20 s1[i]=char((s1[i]-'0')%10+'0'); 21 if(i) 22 s1[i-1]++; 23 else 24 s1='1'+s1; 25 } 26 } 27 return s1; 28 } 29 30 int main() 31 { 32 string str[10000]; 33 str[1]="1"; 34 str[2]="1"; 35 str[3]="1"; 36 str[4]="1"; 37 int n; 38 for(n=5;n<7039;n++) 39 { 40 str[n]=sum(sum(str[n-1],str[n-2]),sum(str[n-3],str[n-4])); 41 } 42 int x; 43 while(scanf("%d",&x)!=EOF) 44 { 45 cout<<str[x]<<endl; 46 } 47 return 0; 48 }