1. 程式人生 > >[Al]演算法:有n級階梯,每次走1步或2步,最多有多少種走法

[Al]演算法:有n級階梯,每次走1步或2步,最多有多少種走法

 @Filename   :   floor.c
*   @Author     :   Mr.Zhong
*   @Date       :   2018-11-02
*   @Description:   n級階梯,每次走一步或2步,最多有多少種走法
*   @Analysis   :   斐波那契數列 -> 遞迴思維
***************************************************************/
#include <iostream>
 
using namespace std;
 
int getStepNum(int n)
{
    if(n < 0)
        return 0;
    if(n == 0 || n ==1 || n ==2)
        return n;
    if(n > 2)
        return getStepNum(n-1)+getStepNum(n-2);
}
 
int main()
{
    int levels;
    cout<<"Please enter how many levels of stairs :"<<endl;
    cin>>levels;
 
    int method = getStepNum(levels);
    cout<<"There are  "<<method<<" methods in total"<<endl;
    return 1;
}
 
--------------------- 
作者:Tobiu 在這裡插入程式碼片
來源:CSDN 
原文:https://blog.csdn.net/localhostcom/article/details/83662968 
版權宣告:本文為博主原創文章,轉載請附上博文連結!