知识:斐波那契数列,0,1,1,2,3,5,8,13......即f(n)=f(n-1)+f(n-2)。
题目:输入n,输出数列的第n项。
思路:直接算第n项是几,用for循环
class Solution {
public:
int Fibonacci(int n) {
int result=0;
int pre=1;
int prepre=0;
if(n==0)
return 0;
if(n==1)
return 1;
for(int i=2;i<=n;i++)
{
result=pre+prepre;
prepre=pre;
pre=result;
}
return result;
}
};