青蛙跳台阶,可以一次1阶或2阶,n个台阶的跳法,即f(n-1)+f(n-2)第一次跳1阶和第一次跳2阶的跳法加起来。
#include<iostream>
using namespace std;
long long Fibonacci(unsigned int n)
{
int result[3]={0,1,2};
if(n<=2)
return result[n];
long long fibNminusOne = 2;
long long fibNminusTwo = 1;
long long fibN = 0;
for(unsigned int i = 3;i <= n;i++)
{
fibN = fibNminusOne + fibNminusTwo;
fibNminusTwo = fibNminusOne;
fibNminusOne = fibN;
}
return fibN;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n;
cin >> n;
cout << " 共有" << Fibonacci(n) <<"种跳法" << endl;
system("pause");
return 0;
}