fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. const int N = 5;
  7.  
  8. double A[N] = { 1, 2, 3, 4, 5 };
  9.  
  10. double calc(double x, double* a, int n = 0)
  11. {
  12. if (n == N-1) return a[n];
  13. return calc(x,a,n+1)*x+a[n];
  14. }
  15.  
  16. double series(double x, double* a)
  17. {
  18. double sum = 0.0;
  19. for(int i = N-1; i >= 0; --i)
  20. sum = sum*x + a[i];
  21. return sum;
  22. }
  23.  
  24. int main(int argc, const char * argv[])
  25. {
  26. for(double x = 0.0; x < 5.0; x += 0.5)
  27. {
  28. cout << setw(8) << calc(x,A) << " " << setw(8) << series(x,A) << endl;
  29. }
  30. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
       1          1
  3.5625     3.5625
      15         15
 49.5625    49.5625
     129        129
 282.562    282.562
     547        547
 966.562    966.562
    1593       1593
 2485.56    2485.56