hpuoj【1055】斐波那契



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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值