返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int fai(int n)
  5. {
  6.     if(n<2)    //邊界條件
  7.         return n;
  8.     else
  9.         return fai(n-2)+fai(n-1);
  10. }
  11. int main()
  12. {
  13.     int n;
  14.     cout<<"請問要推算費氏數列到第幾項次? ";
  15.     cin>>n;
  16.     for(int i=0; i<=n; i++)
  17.         cout<<fai(i)<<" ";
  18.     cout<<endl;
  19.     system("pause");
  20.     return 0;
  21. }
複製代碼

TOP

返回列表