遞歸--練習6--noi1755菲波那契數列
阿新 • • 發佈:2017-06-22
ace ac代碼 std 題目 ++ pen names tdi problem
遞歸--練習6--noi1755菲波那契數列
一、心得
二、題目
1755:菲波那契數列
- 總時間限制:
- 1000ms
- 內存限制:
- 65536kB
- 描述
- 菲波那契數列是指這樣的數列: 數列的第一個和第二個數都為1,接下來每個數都等於前面2個數之和。
給出一個正整數a,要求菲波那契數列中第a個數是多少。 - 輸入
- 第1行是測試數據的組數n,後面跟著n行輸入。每組測試數據占1行,包括一個正整數a(1 <= a <= 20)
- 輸出
- 輸出有n行,每行輸出對應一個輸入。輸出應是一個正整數,為菲波那契數列中第a個數的大小
- 樣例輸入
-
4 5 2 19 1
- 樣例輸出
-
5 1 4181 1
三、AC代碼
1 /* 2noi1755菲波那契數列 3 */ 4 #include <iostream> 5 #include <cstdio> 6 using namespace std; 7 int f(int n){ 8 if(n==1||n==2) return 1; 9 else return f(n-1)+f(n-2); 10 } 11 int main(){ 12 //freopen("in.txt","r",stdin); 13 int n; 14 cin>>n; 15 for(int i=1;i<=n;i++){16 int a; 17 cin>>a; 18 int ans=f(a); 19 cout<<ans<<endl; 20 } 21 return 0; 22 }
遞歸--練習6--noi1755菲波那契數列