您的位置:首页 > 其它

[2041]: 超级楼梯(递归)

2016-03-07 00:50 411 查看
Problem Description

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

Input

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

Output

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

Sample Input

2

2

3

Sample Output

1

2

其实这道题不难,思考问题的突破点在于:若每次只能跨上一级或二级,为什么这么说呢?

请思考:问第m级的可能性,那么就要考虑影响因素:第m-1(跨上一级到第m级)和m-2(跨上二级到第m级)级,所以第m级的可能性就是他前面两者的和

综上所述,这道题应该用递归的思想去思考并解决

/*
author : Yangchengfeng
*/

#include<stdio.h>
#define N 41

int test(int level);

int main()
{
int n;
scanf("%d", &n);
while(n--){
int m;
scanf("%d", &m);
printf("%d\n", test(m));
}
}

int test(int level){
int m
= {0, 1, 1};
int i = 3;
for(; i<=level ; i++){
m[i] = m[i-1] + m[i-2];
}
return m[level];
}




/*
author : Yangchengfeng
*/

#include<stdio.h>
#define N 41

int test(int level);

int main()
{
int n;
scanf("%d", &n);
int m
= {0, 1, 1};
int i = 2;
for(; i<=40; i++){
m[i] = m[i-1] + m[i-2]; // 由于题目限定最大阶数,所以全部算出来还是可行的措施
}
while(n--){
int k;
scanf("%d", &k);
printf("%d\n", m[k]);
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: