您的位置:首页 > 其它

有n个台阶,如果一次只能上1个或2个台阶,求一共有多少种上法

2013-07-09 19:48 471 查看
// n级台阶,求多少种跳法.cpp : Defines the entry point for the console application.
//
/*
思路:
如果只有一级台阶,n=1,很明显只有一种跳法
如果有两级台阶,n=2,则有两种跳法,一种是跳两下1级,一种是直接跳两级
那么我们来看看如果有n层台阶,可以怎么跳:
n层台阶可以是这么够成的
1.第n层台阶是从第n-1层跳1级上来的
2.第n层台阶是从第n-2层直接跳2级上来的
所以可以得到n层的跳法总数是F(n)=F(n-1)+F(n-2)
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
int Solve(int n)
{
if(n==1)
return 1;
if(n==2)
return 2;
return Solve(n-1)+Solve(n-2);
}

int _tmain(int argc, _TCHAR* argv[])
{
int num=Solve(4);
cout<<num<<endl;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐