HDU 2041 (斐波那契数列,水题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2041

思路:举几个例子就可以看得出是斐波那契数列,用递归做就行

AC代码

#include<iostream>
#include<cstdio>
using namespace std;
int fun(int x)
{
	if(x==1) return 1;
	if(x==2) return 1;
	if(x==3) return 2;
	else return fun(x-1)+fun(x-2);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int m;
		cin>>m;
		cout<<fun(m)<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zvenWang/article/details/82150182