Description
1202年,意大利数学家斐波那契出版了他的<<算盘全书>>,在书中第一次提到了著名的Fibonacci数列,定义如下:
f(1)=1,f(2)=1,f(n)=f(n-1)+f(n-2) n>2
现在你的任务是求出Fibonacci数列的第n项。
Input
输入仅一个整数,表示n的值。
Output
程序运行结束时,将Fibonacci数列的第n项输出。
我们保证输出的结果不会超过1000位数,并且在测试数据中40%的结果不超过10位。
Sample Input
20
Sample Output
6765
KEY:大整数的加法,用数组;
Source:#include<iostream>
using namespace std;
int a[1001];
int b[1001];
int c[1001];
int n;
void fun()
...{
a[0]=b[0]=1;
int i,j,t,e;
for(i=3;i<=n;i++)
...{
e=0;
for(j=0;j<=1000;j++)
...{
t=a[j]+b[j]+e;
c[j]=t%10;
e=t/10;
}
for(j=0;j<=1000;j++)
...{
a[j]=b[j];
b[j]=c[j];
![]()
}
}
for(j=1000;c[j]==0;j--) ;
for(;j>=0;j--)
...{
cout<<c[j];
}
![]()
cout<<endl;
}
int main()
...{
cin>>n;
if(n==1)
...{
cout<<"1"<<endl;
exit(1);
}
if(n==2)
...{
cout<<"1"<<endl;
exit(1);
}
fun();
return 0;
}