fork(1) download
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4. int tong(int n);
  5. int main()
  6. {
  7. for (auto i = 1; i <= 5; i ++) {
  8. cout << "N = " << i << ", ";
  9. cout << "S = " << tong(i) << endl;
  10. }
  11. return 0;
  12. }
  13.  
  14. int tail_tong(int n, int result) {
  15. if (n > 1) {
  16. return tail_tong(n - 1, result + n * n);
  17. }
  18. return result;
  19. }
  20.  
  21. int tong(int n)
  22. {
  23. return tail_tong(n, 1);
  24. }
  25.  
  26.  
Success #stdin #stdout 0s 4536KB
stdin
Standard input is empty
stdout
N = 1, S = 1
N = 2, S = 5
N = 3, S = 14
N = 4, S = 30
N = 5, S = 55