Description: 楼梯有n阶,可以一阶一阶走或者两阶两阶走,给定台阶数,问共有几种走法
Input: 台阶数 n
Output: 走法 m
分析:对于第n + 1级台阶,有两种走到的方法:1)由第n阶走一步 2)由第n - 1阶跨2步
则走到n + 1阶的方法数 f(n + 1) = f(n) + f(n - 1)
实现:下例用递归方式实现
#include<cstdio>
int method(int n)
{
if(n == 1)
return 1;
else if(n == 2)
return 2;
else
return method(n - 1) + method(n - 2);
}
int main()
{
printf("%d", method(7));
return 0;
}