HDU 2041--超级楼梯(递推求解)

Description

有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
 

Input

输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。
 

Output

对于每个测试实例,请输出不同走法的数量
 

Sample Input

2 2 3
 

Sample Output

1 2
 
这道题从逆向考虑,第m级的走法数量,就是第m-1级和第m-2级各自走法数量之和
 
 1 #include<iostream>
 2 #include<cmath>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 
 7 int main()
 8 {
 9           int n;
10           cin>>n;
11           while(n--)
12           {
13                     int t;
14                     cin>>t;
15                     int a[50];
16                     a[1] = 1;
17                     a[2] = 2;
18                     for(int i = 3;i<t;i++)
19                               a[i] = a[i-1]+a[i-2];
20                     cout<<a[t-1]<<endl;
21           }
22 
23 }
原文地址:https://www.cnblogs.com/qlky/p/4958421.html