#include<cstdio> #include<string> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; char s[5002][1200]; int main() { char a[2000],b[2000],c[2000],d[2000]; int i,j,k,l,m=6,n; strcpy(s[0],"0"); strcpy(s[1],"1"); strcpy(s[2],"1"); strcpy(s[3],"2"); strcpy(s[4],"3"); strcpy(s[5],"5"); strcpy(a,"5"); strcpy(b,"3"); while(m!=5001) { if(strlen(a)==strlen(b)) { int r=0; for(i=0;i<strlen(a);i++) { c[i]=(((a[i]-'0')+(b[i]-'0')+r)%10)+'0'; r=((a[i]-'0')+(b[i]-'0')+r)/10; } if(r>0) { c[i++]=r+'0'; r=0; } c[i]='\0'; j=0; for(i=strlen(c)-1;i>=0;i--) { d[j++]=c[i]; } d[j]='\0'; strcpy(s[m++],d); strcpy(b,a); strcpy(a,c); } else if(strlen(a)>strlen(b)) { int r=0; for(i=0;i<strlen(b);i++) { c[i]=(((a[i]-'0')+(b[i]-'0')+r)%10)+'0'; r=((a[i]-'0')+(b[i]-'0')+r)/10; } for(i=strlen(b);i<strlen(a);i++) { c[i]=(((a[i]-'0')+r)%10)+'0'; r=((a[i]-'0')+r)/10; } if(r>0) { c[i++]=r+'0'; r=0; } c[i]='\0'; j=0; for(i=strlen(c)-1;i>=0;i--) { d[j++]=c[i]; } d[j]='\0'; strcpy(s[m++],d); strcpy(b,a); strcpy(a,c); } } while(scanf("%d",&n)==1) { printf("The Fibonacci number for %d is %s\n",n,s[n]); } return 0; }
Sunday, August 24, 2014
UVa - 495 - Fibonacci Freeze
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment