九度 OJ 1092 Fibonacci

時間限制:1 秒

內存限制:32 兆

特殊判題:

提交:2110

解決:1516

題目描述:

    The Fibonacci Numbers{0,1,1,2,3,5,8,13,21,34,55...} are defined by the recurrence: 
    F0=0 F1=1 Fn=Fn-1+Fn-2,n>=2 
    Write a program to calculate the Fibonacci Numbers.

輸入:

    Each case contains a number n and you are expected to calculate Fn.(0<=n<=30) 。

輸出:

   For each case, print a number Fn on a separate line,which means the nth Fibonacci Number.

樣例輸入:
1
樣例輸出:
1
來源:
2006年上海交通大學計算機研究生機試真題
遞歸

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int Fibonacci(int n)
{
    if(n==0) return 0;
    else if(n==1) return 1;
    else return Fibonacci(n-1)+Fibonacci(n-2);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        printf("%d\n",Fibonacci(n));
    }
}
 
/**************************************************************
    Problem: 1092
    User: th是個小屁孩
    Language: C++
    Result: Accepted
    Time:30 ms
    Memory:1520 kb
****************************************************************/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章