超级楼梯 HDU - 2041

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

Input

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

Output

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

Sample Input

2
2
3

Sample Output

1
2

AC(递推题)

Select Code

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    std::ios::sync_with_stdio(false);
    int m, a[100], n, i;
    a[2] = 1, a[3] = 2;
    cin>>n;
    while(n--)
    {
        cin>>m;
        for(i = 4;i<=m;i++)
        {
            a[i] = a[i-1]+a[i-2];
        }
        cout<<a[m]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41524782/article/details/81782188