走臺階問題算法

/*
分析:

1 步臺階只有1種走法(1)

2步臺階2種(11、2)

3步臺階有3種(111、12、21)

4 步臺階有5種(1111、112、121、211、22)

5 步臺階有8種(11111、1112、1121、1211、122、2111、212、221)

6步臺階有13種(111111、11112、11121、11211、1122、12111,1212、1221、2111、2112、2121、2211、222)

*/


int fun(int n)
{
 
 if(n==1) return 1;
 if(n==2) return 2;
 //if(n==3) return 4;
  
 

 return /* fun(n-3)*/ fun(n-2)+fun(n-1);
 
}

#include"iostream"
using namespace std;

void main(void)
{
int n;
cout<<"Enter the num of steps:";
cin>>n;
cout<<fun(n)<<" ";

}

發佈了36 篇原創文章 · 獲贊 0 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章