2018年4月1日 星期日

[C_RU10-中] 爬樓梯(C++)

[C_RU10-中] 爬樓梯

1. 問題描述: 
一至二樓有 8 級樓梯,某人上樓,每次可跨 1 級或 2 級,不同上樓的方法有幾種?
CRU10.JPG
輸入說明:
輸入樓梯之級數 n(3 ≦ n ≦ 20) 。
輸出說明:
輸出不同上樓的方法總數。
範例 :
Sample InputSample Output
33
13377


  1. #include <iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4.    
  5. int fib(int n)  
  6. {  
  7.     if(n==1 || n == 2)  
  8.     {  
  9.         return(n);  
  10.     }  
  11.     else  
  12.     {  
  13.         return fib(n-1) + fib(n-2);  
  14.     }  
  15. }  
  16.    
  17. int main() {  
  18.     // [C_RU10-中] 爬樓梯  
  19.     // 費式數列  
  20.     double N;  
  21.     cin >> N;  
  22.      for (double i=1; i<= N; i++)  
  23.      {  
  24.         if(i == N)  
  25.         {  
  26.             cout << fib(i) << endl;  
  27.         }  
  28.      }  
  29.     return 0;  
  30. }  

沒有留言:

張貼留言