Fibonacci使用递归和循环实现
#include<stdio.h>
double Fibonacci(int i);
double Fibonacci_(int i);
int main(void)
{
int i;
printf("Enter the number i:\n");
scanf("%d", &i);
printf("%f %f",Fibonacci(i),Fibonacci_(i));
return 0;
}
double Fibonacci(int i)
{
double k=1;
double g;
for(double n=0;i-1>=0;i--)
{
g=k+n;
n=k;
k=g;
}
return g;
}
double Fibonacci_(int i)
{
double k;
if(i==2) return 2;
else if(i==1) return 1;
else if(i<=0) return 0;
else
k=Fibonacci_(i-1)+Fibonacci_(i-2);
return k;
}
posted on 2017-12-30 10:54 MACHINE_001 阅读(119) 评论(0) 编辑 收藏 举报