【C系列4.4】函数训练之斐波那契数列 1972
Time Limit: 1 s Memory Limit: 128 MB
Submission:202 AC:154 Score:10.00
Description
今天mwy老师教了cyn小朋友斐波那契数列,cyn表示很好奇,于是他决定深入研究一下,你能帮帮他吗?(主函数已经给出,如果提交的不是c语言则需要提交全部代码)
#include<stdio.h>
int f(int n);
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
printf("%d\n ",f(n));
}
return 0;
}
Input
第一行输入一个T,表示有T组数据。
接下来有T行,每行只有一个整数n(1 <= n <=30),代表要找到斐波那契数列的第n个数字。
Output
对于每一个n,输出其对应的数字。
Samples
input:
4
1
2
3
4
output:
1
1
2
3
下附AC代码:
#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