- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int calcu(int);
- int total(int);
- int main()
- {
- int x;
- cout<<"請輸入欲推算總和到費氏數列第幾項次: ";
- cin>>x;
- cout<<x<<"費氏數列中前"<<x<<"個數的總和為"<<total(x)<<endl<<endl;
- system("pause");
- return 0;
- }
- int total(int x)
- {
- int tot=0;
- for(int i=1; i<=x; i++)
- tot=tot+calcu(i);
- return tot;
- }
- int calcu(int x)
- {
- if(x<=1)
- return x;
- else
- return calcu(x-1)+calcu(x-2);
- }
複製代碼 |