斐波那契数列第n项
#include <stdio.h>
#include <string.h>
int fibonacci(int n)
{
int index,temp1,temp2,temp[2];
temp[0] = 1;
temp[1] = 1;
if(n < 1)
{
printf("error n\n");
return -1;
}
if(n == 1)
return temp[0];
if(n == 2)
return temp[1];
for(index = 3;index <= n;index++){
temp1=temp[0];
temp2=temp[1];
temp[0]=temp2;
temp[1]=temp1+temp2;
}
return temp[1];
}
int main(void)
{
int n;
printf("please input n:\n");
scanf("%d",&n);
printf("the %dth is: %d\n",n,fibonacci(n));
return 0;
}
这个程序受到数据类型所能表示的数值范围限制。
斐波那契数列第n项
最新推荐文章于 2022-11-29 17:19:59 发布