c语言问题 n=1或2,f(n)=1;n>2,f(n)=f(n-1)+f(n-2)。不用递归法求。谢谢!

2024-12-29 18:36:50
推荐回答(1个)
回答1:

#include
int fibo(int n)
{
int a=0,b=1,c,i;
if(n<=1)
return n;
else
{
for(i=2;i<=n;i++)
{
c=a+b;
a=b;
b=c;
}
return c;
}
}
void main()
{
int n;
scanf("%d",&n);
printf("fibo(%d)=%d",n,fibo(n));
}