ACM:蓝桥杯:斐波那契数

来源:互联网 发布:linux使用 编辑:程序博客网 时间:2024/05/01 10:29

描述
无穷数列1,1,2,3,5,8,13,21,34,55…称为Fibonacci数列,它可以递归地定义为
F(n)=1 ………..(n=1或n=2)
F(n)=F(n-1)+F(n-2)…..(n>2)
现要你来求第n个斐波纳奇数。(第1个、第二个都为1)
输入
第一行是一个整数m(m<5)表示共有m组测试数据
每次测试数据只有一行,且只有一个整形数n(n<20)
输出
对每组输入n,输出第n个Fibonacci数
样例输入
3
1
3
5
样例输出
1
2
5

#include<iostream>using namespace std;int f(int x){    if(x==1||x==2)  return 1;    else if(x>0)    return (f(x-1)+f(x-2));    else    return 0;}int main(){    int x;    cin>>x;    while(x--)    {        int y;        cin>>y;        cout<<f(y)<<endl;    }} 
0 0
原创粉丝点击