请问:C语言的Fibonacci题会写吗?
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/10 15:10:18
请问:C语言的Fibonacci题会写吗?
Description
Fibonacci数列定义为(1,1,2,3,5,8,.),即每个元素是前两个元素的和.如果一个Fibonacci数与所有小于它的Fibonacci数互质,那么称之为Fibonacci质数.现在要求你输出前n个Fibonacci数The Fibonacci Numbers {0,1,1,2,3,5,8,13,21,34,55 ...} are defined bythe recurrence:F(0)=0 F(1)=1 F(i)=F(i-1)+F(i-2) Write a program to calculatethe Fibonacci Numbers.
Input
Thefirst line of the input file contains a single integer T,the number of testcases.The following T lines,each contains an integer n ( 0
Description
Fibonacci数列定义为(1,1,2,3,5,8,.),即每个元素是前两个元素的和.如果一个Fibonacci数与所有小于它的Fibonacci数互质,那么称之为Fibonacci质数.现在要求你输出前n个Fibonacci数The Fibonacci Numbers {0,1,1,2,3,5,8,13,21,34,55 ...} are defined bythe recurrence:F(0)=0 F(1)=1 F(i)=F(i-1)+F(i-2) Write a program to calculatethe Fibonacci Numbers.
Input
Thefirst line of the input file contains a single integer T,the number of testcases.The following T lines,each contains an integer n ( 0
#include <stdio.h>
int F(int);
int main() {
int n;
scanf("%d\n", &n);
while (n--) {
int a;
scanf("%d\n", &a);
printf("%d\n", F(a));
}
return 0;
}
int F(int n) {
if (n == 0)
return 0;
if (n == 1 || n == 2)
return 1;
else
return F(n - 1) + F(n - 2);
}
再问: 好像少输最后一个数唉!
再答: 经测试无误。
int F(int);
int main() {
int n;
scanf("%d\n", &n);
while (n--) {
int a;
scanf("%d\n", &a);
printf("%d\n", F(a));
}
return 0;
}
int F(int n) {
if (n == 0)
return 0;
if (n == 1 || n == 2)
return 1;
else
return F(n - 1) + F(n - 2);
}
再问: 好像少输最后一个数唉!
再答: 经测试无误。