#include<stdio.h>
#define Method 0
// 1:迭代法 0:递归法
#if Method
// 第一种:迭代法
long fibonacci(int i)
{
if(i<=2)
{
return 1;
}
long pre_pre_res,pre_res,result;
pre_pre_res = pre_res = 1;
while(i>2)
{
result = pre_res + pre_pre_res;
pre_pre_res = pre_res;
pre_res = result;
i--;
}
return result;
}
#else
//第二种:递归法
long fibonacci(int i)
{
if(i<=2)
{
return 1;
}
return fibonacci(i-1)+fibonacci(i-2);
}
#endif
//主函数:
int main(void)
{
int N,i;
printf("please input a nummber:\n");
scanf("%d",&N);
for(i=0;i<=N;i++)
{
printf("num:%ld\n",fibonacci(i) );
}
return 0;
}
01-21
8863
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-20
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)