斐波那契数列:0,1,1,2,3,5,8...
- 递归
int fib(int n){
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
return fib(n-2) + fib(n-1);
}
- 迭代
int fib(int n){
if(n == 0)
return 0;
else if(n == 1)
return 1;
int num;
int fib_1 = 0;
int fib_2 = 1;
for(int i=2; i<=n; i++){
num = fib_1 + fib_2;
fib_1 = fib_2;
fib_2 = num;
}
return num;
}