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

2023-07-11,,

// 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;
}

有n个台阶,如果一次只能上1个或2个台阶,求一共有多少种上法的相关教程结束。

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

下载本文的Word格式文档,以方便收藏与打印。