【C系列4.4】函數訓練之斐波那契數列 1972
Description
今天mwy老師教了cyn小朋友斐波那契數列,cyn表示很好奇,於是他決定深入研究一下,你能幫幫他嗎?(主函數已經給出,如果提交的不是c語言則需要提交全部代碼)
#include<stdio.h>
Input
第一行輸入一個T,表示有T組數據。
接下來有T行,每行只有一個整數n(1 <= n <=30),代表要找到斐波那契數列的第n個數字。
Output
對於每一個n,輸出其對應的數字。
Samples
#include<stdio.h>
int f(int n);
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long n;
scanf("%d",&n);
printf("%d\n",f(n));
}
return 0;
}
int f(int n) {
return (1 == n || 2 == n) ? 1 : (f(n - 1) + f(n - 2));
}
原題鏈接:http://acm.hznu.edu.cn/OJ/problem.php?cid=1092&pid=5