超级楼梯
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 68779 Accepted Submission(s): 35064
Problem Description
有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
Input
输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。
Output
对于每个测试实例,请输出不同走法的数量
Sample Input
2 2 3
Sample Output
1 2
Author
lcy
Source
Recommend
题意:一次可以跨一级或两级,一开始在第一级,问跨到第n级又多少种方法?
思路:最后一步可能是跨了一级也可能是跨了两级
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
#define IO ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
using namespace std;
int main()
{
ll a[1010], n, m;
a[1] = 1; a[2] = 1;
for(int i = 3; i < 60; i++){
a[i] = a[i-1]+a[i-2];//最后一步可能是跨了一级,也可能是跨了两级
}
cin >> n;
while(n--){
cin >> m;
cout << a[m] << endl;
}
return 0;
}