DP 1010

来源:互联网 发布:阿里云磁盘快照 编辑:程序博客网 时间:2024/05/16 08:12
Problem Description
有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
 

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

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

Sample Input
223
 

Sample Output
12

 

解题思路:从第4阶开始,第i阶的方法为i-1的方法加上i-2的方法

感想:依旧是水题……

AC代码:

#include<iostream>using namespace std; int main(){int a[1000];int n;a[1] = 1;a[2] = 1;a[3] = 2;cin >> n ;while(cin >> n){if(n>=1&&n<=3)cout << a[n] << endl ;else {for(int i = 4;i <= n ; i ++)a[i] = a[i - 1] + a[i -2];cout << a[n] << endl;}}return 0;}


0 0