(C语言)用递归方法编写求斐波那契数列的函数

2024-12-16 05:00:31
推荐回答(2个)
回答1:

#include
long func(long n)
{
if(n==0||n==1)return n;
else return func(n-1)+func(n-2);
}
main()
{
long n;
printf("please input n:");
scanf("%ld",&n);
printf("the result is %ld",func(n));
}

回答2:

long Fib2(int n)
{ long int oneBack, twoBack, current;
int i;_
if(n == 0 || n == 1) return n;
else
{ oneBack = 1;
twoBack = 0;
for(i = 2; i <= n; i++)
{ current = oneBack + twoBack;
twoBack = oneBack;
oneBack = current;
}
return current;
}
}