返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int total(int);
  5. int main()
  6. {
  7.     int x;
  8.     cout<<"1+2+3+...+10="<<total(10)<<endl;
  9.     cout<<"1+2+3+...+100="<<total(100)<<endl;
  10.     system("pause");   
  11.     return 0;
  12. }
  13. int total(int x)
  14. {
  15.     if(x<=1)
  16.        return x;
  17.     else
  18.        return x+total(x-1);
  19. }
複製代碼

TOP

返回列表