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