树老师爬楼梯,每次可以爬一阶或者两阶,共有n阶楼梯,问有多少走法。
爬n阶台阶可以分为:1.先爬一阶台阶,再爬n-1阶台阶。2.先爬两阶台阶,再爬n-2阶台阶。
最后得出递归表达式
f(n) = f(n-1)+g(n-2);
#include <iostream>
#define N 100005
using namespace std;
int f(int x)
{
if(x==1||x==2)
return x;
else
return f(x-1)+f(x-2);
}
int main()
{
int n;
while(cin>>n)
cout<<f(n)<<endl;
return 0;
}