题目:
用递归算法计算斐波拉契级数列中第n 项的值。从第一项起,斐波拉契级数序列为1,1,
2,3,5,8,13,21,……,例如,若给n 输入7,该项的斐波拉契级数值为13。
代码展示:
#include <stdio.h>
int fibonacci (int n)
{
if (n<=2)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main()
{
int n;
printf("Please enter the value of the nth term in the Fibonacci series:\n");
scanf("%d", &n);
printf("\n");
printf("fib(%d)=%3d",n, fibonacci(n));
return 0;
}