打印Fibonacci数列:
Fibonacci数列:0,1,1,2,3,5,8,13,21,… 求第i项的值(每一项的值为前两项的和)
Fibonacci(i)=Fibonacci(i-1)+Fibonacci(i-2)
代码:
#include <stdio.h>
long fb(int n);
int main()
{
int i;
unsigned long z;
do
{
scanf("%d",&i);
z=fb(i);
printf("fb(%d)=%lu\n",i,z);
}while(i>0);
}
long fb (int n)
{
if(n==1||n==2)
return n-1;
else
return fb(n-1)+fb(n-2);
}