有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
Input
输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。
Output
对于每个测试实例,请输出不同走法的数量
Sample Input
2 2 3
Sample Output
1 2
思路:
m=2,1次;
m=3,2次;
m=4,3次;
m=5,5次;
m=6,8次;
m=n,m[n-2]+m[n-1]次;
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
int step[50];
step[1]=0;//预处理打表
step[2]=1;
step[3]=2;
for(int i=4;i<50;i++){
step[i]=step[i-1]+step[i-2];
}
int n;
scanf("%d",&n);
while(n--){
int m;
scanf("%d",&m);
printf("%d\n",step[m]);
}
return 0;
}