1055: 斐波那契 [水题]
时间限制: 1 Sec 内存限制: 128 MB提交: 252 解决: 106
题目描述
Do you know the Fibonacci sequence?
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones:
1、1、2、3、5、8、13、21……
The sequence Fn of Fibonacci numbers is defined by the recurrence relation:
Fn=Fn−1+Fn−2
Give you a positive integer n
,I want to know Fn.
输入
Input includes multiple cases.
Each line is a positive integer n
.
(0<n<40)
输出
For each case:
Print a integer number in a line, is the Fn
.
样例输入
2
3
4
样例输出
1
2
3
程序如下:
#include<cstdio>
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int a[50],i;
a[1]=a[2]=1;
if(n==1||n==2)
printf("1\n");
else
{
for(i=3;i<=n;i++)
a[i]=a[i-1]+a[i-2];
printf("%d\n",a[n]);
}
}
return 0;
}