您的位置:首页 > 其它

超级台阶

2015-07-06 22:36 316 查看
描述

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

注:规定从一级到一级有0种走法。

输入

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

输出

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

样例输入

2

2

3

样例输出

1

2

很明显,递归思想

// Goupstairs.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;
int Go_up(int m);
int _tmain(int argc, _TCHAR* argv[])
{
int n = 3;
cout << Go_up(3) << endl;
system("pause");
return 0;
}

int Go_up(int m)
{
if (m == 2)
return 2;
if (m == 1)
return 1;
else
return (Go_up(m - 1) + Go_up(m - 2));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: