您的位置:首页 > 编程语言 > C语言/C++

HDU 2041 超级楼梯

2015-07-20 15:17 363 查看
超级楼梯
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

Description

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

Input

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

Output

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

Sample Input

2
2
3

 

Sample Output

1
2

 

分析

第 n 级台阶只能由第 n-1 级台阶走一步或由第 n-2 级台阶走两步达到,所以递推公式为f(n) = f(n-1) + f(n-2)。

AC代码如下

#include <cstdio>
#include <cstring>
#define maxn 42

using namespace std;

long long int a[maxn];<span style="white-space:pre"> </span>//测试数据会爆int,所以用long long int

int main()
{
int n,t;
a[1] = 0;
a[2] = 1;
a[3] = 2;
for(int i = 4 ; i < maxn-1; i++)
a[i] = a[i-1] + a[i-2];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",a
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息