返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int calcu(int);
  5. int total(int);
  6. int main()
  7. {
  8.      int x;
  9.      cout<<"請輸入欲推算總和到費氏數列第幾項次: ";
  10.      cin>>x;
  11.      cout<<x<<"費氏數列中前"<<x<<"個數的總和為"<<total(x)<<endl<<endl;
  12.      system("pause");
  13.      return 0;
  14. }
  15. int total(int x)
  16. {
  17.      int tot=0;
  18.      for(int i=1; i<=x; i++)
  19.          tot=tot+calcu(i);
  20.      return tot;
  21. }
  22. int calcu(int x)
  23. {
  24.      if(x<=1)
  25.         return x;
  26.      else
  27.         return calcu(x-1)+calcu(x-2);
  28. }
複製代碼

TOP

返回列表