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;
}
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;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++ Custom Control控件向父窗体发送对应的消息
- C++中拷贝构造函数的应用详解