#include<stdio.h>
//斐波那契:
//法1:
int Fibon(int n)
{
if(n1||n2)
{
return 1;
}
else
{
return Fibon(n-1)+ Fibon(n-2);
}
}
//法2:
int Fibon1(int n)
{
int f1=1;
int f2=1;
int f3=1;
if(n<3)
{
return f3;
}
for(int i=3;i<=n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
}
return f3;
}
int main()
{
printf("%d\n", Fibon(5));
printf("%d\n", Fibon1(2));
}
注释:法一中的if是边界条件,n等于1或者2;