#include <iostream>
using namespace std;
int f(int n){
if(n == 1){
return 1;
}else if(n == 2){
return 2;
}else{
return f(n-1) + f(n-2);
}
}
int main() {
int n; //n级台阶
while(cin >> n){
cout << f(n) << endl;
}
return 0;
}
测试可得:
f(1)=1,f(2)=2,f(3)=3,f(4)=5,f(5)=8,f(6)=13,f(7)=21 …
可知,跳台阶问题是斐波那契数列问题的变形,即后一个值是前两个值之和。