int fib(int n) {
if(n<=0) {
return 0;
}
if (n==1 || n==2) {
return 1;
}
if (n>2) {
return fib(n-1) + fib(n-2);
}
return 0;
}
int fibo(int n) {
if(n<=0) {
return 0;
}
if (n==1 || n==2) {
return 1;
}
int pre = 1; //fibo(2)
int prepre = 1; //fibo(1)
int current = 2; //fibo(3)
// 记录中间数值
for (int i=3; i<=n; i++) {
current = pre + prepre;
prepre = pre;
pre = current;
}
return current;
}
https://www.cnblogs.com/iwiniwin/p/10798884.html