A Fibonacci sequence is calculated by adding the previous and the sequence, with the first and both 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 would contain an integers. Process to end of file.
Output
For each case, the output of the result in a line.
Sample Input
100
Sample Output
4203968145672990846840663646note:no generated Fibonacci number in excess of 2005 digits would be in the test data, ie. F () = 66526 has 5 digits.
#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<string.h>#include<malloc.h>using namespacestd;voidAddCharAChar* B,Char*c) { inti,j,k,max,min,n,temp; Char*s,*pmax,*Pmin; Max=strlen (a); Min=strlen (b); if(max<min) {Temp=Max; Max=min; Min=temp; Pmax=b; Pmin=A; } Else{Pmax=A; Pmin=b; } s=(Char*)malloc(sizeof(Char) * (max+1)); s[0]='0'; for(i=min-1, j=max-1, K=max; i>=0; I--, J--, k--) S[k]=pmin[i]-'0'+Pmax[j]; for(; j>=0; J--, k--) S[k]=Pmax[j]; for(I=max; i>=0; i--) if(s[i]>'9') {S[i]-=Ten; S[i-1]++; } if(s[0]=='0') { for(i=0; i<=max; i++) C[i-1]=S[i]; C[i-1]=' /'; } Else { for(i=0; i<=max; i++) C[i]=S[i]; C[i]=' /'; } Free(s);}
Chara[8001][2505];intMainvoid){ intI,n; for(i=1; i<=4; i++) strcpy (A[i],"1"); for(i=5;i<8000; i++) { Charx[2505],y[2505]; Add (A[i-1],a[i-2],x); Add (A[i-3],a[i-4],y); Add (X,y,a[i]); } while(cin>>N) {cout<<a[n]<<Endl; } return 0;}
Fibonacci High-precision addition