输出斐波那契(Fibonacci数列)的前n项。用递推实现:
#include <stdio.h>
#include <stdlib.h>
void Fibonacci(int n)
{
int a1=1,a2=1,a3,i;
if(n==1)printf("%d\n",a1);
else if(n==2)printf("%d %d\n",a1,a2);
else
{
printf("%d %d ",a1,a2);
for(i=3; i<=n; i++)
{
a3=a1+a2;
printf("%d ",a3);
a1=a2;
a2=a3;
}
}
}
int main()
{
int n;
scanf("%d",&n);
Fibonacci(n);
return 0;
}
来源:https://blog.csdn.net/weixin_43886377/article/details/101122425