航电 2041 超级楼梯
超级楼梯
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 47105 Accepted Submission(s): 24020
Problem Description
有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
Input
输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。
Output
对于每个测试实例,请输出不同走法的数量
Sample Input
2 2 3
Sample Output
1 2
Author
lcy
Source
利用递推关系,就是Fibonacci数列,面对第n级台阶,可以选择一步跨上,也可以跨两步上去,所以对于一步的是f(n
- 1),对于两步的是f(n - 2),所以得到递推关系是f(n) = f(n - 1) + f(n - 2)
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX_N 205
using namespace std;
int ans[MAX_N];
void pre() {
ans[1] = 1;
ans[2] = 2;
for (int i = 3; i < 50; i++) {
ans[i] = ans[i - 1] + ans[i - 2];
}
}
int main() {
int t, n;
pre();
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
printf("%d\n", ans[n - 1]);
}
return 0;
}

浙公网安备 33010602011771号