题目:一个台阶总共有n级,如果一次可以跳1级,也可以跳2级。求总共有多少总跳法。
f(n)=f(n-1)+f(n-2),变成求费伯纳西数列
//跳台阶
#include<iostream>
using namespace std;
int step_two(int num_of_stair){
int before=1;
int after=2;
int total;
if(num_of_stair==1) return 1;
else if(num_of_stair==2) return 2;
else{
for(int i=3;i<=num_of_stair;i++){
total=before+after;
before=after;
after=total;
}
return total;
}
}
int main(void){
int num;
cin>>num;
cout<<step_two(num)<<endl;
system("pause");
return 0;
}