hpuoj【1055】斐波那契

来源:互联网 发布:js设置div颜色 编辑:程序博客网 时间:2024/04/30 11:51



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=Fn1+Fn2

Give you a positive integern

,I want to knowFn

.

输入

Input includes multiple cases.

Each line is a positive integern

.

(0<n<40)

输出

For each case:

Print a integer number in a line, is theFn

.

样例输入

234

样例输出

123


程序如下:

#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;}


原创粉丝点击