A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.
f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2)
Your task is to take a number as input, and print that Fibonacci number.
Inputa positive integer number N. Output
print the Nth Fibonacci number.
Sample Input 100 Sample Output 354224848179261915075 解析:让我们求第N个斐波那契数,高精度。我们运用高精度a+b的做法即可,开3个数组,a[ ],b[ ],c[ ],让c[]=a[]+b[],计算完之后,我们再将a[ ]->b[ ],c[ ]->a[ ],然后再将c[ ]清空,为下一轮a[ ]+b[ ]=c[ ],如此反复,实现f(n)=f(n-1)+f(n-2).然后计算到第N项,将答案数组c[ ]逆序输出即可。#includeint a[1005],b[1005],c[1005];//实现a[]+b[]=c[],c[]作为答案数组 int main() { int n,x,z,p,i,s; while(~scanf("%d",&n)){ x=1,p=1,a[0]=1,b[0]=1; //初始一下,x表示a[]的位数,p表示c[]的位数 if(n==1||n==2){ //众所周知,直接输出 printf("1n"); continue; } for(i=3;i<=n;i++){ for(s=0;s =10) c[z+1]+=c[z]/10; //c[]逐位判断进位 c[z]%=10; } while(c[p]>0){ //核心 p++; //之前所有进位都堆到了,c[p]>0,就像55+55=110,位数肯定+1 while(c[p-1]>=10) c[p]+=c[p-1]/10,c[p-1]%=10,p++; //c[p]已经是当前最高位了,如果>=10,那么继续进位,c[]位数也就+1 } for(z=0;z =0;i--) printf("%d",c[i]),c[i]=0,a[i]=0,b[i]=0;//边输出边初始化 printf("n"); } return 0; }



