fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int getCountSlag(int x, double eps) {
  7. double exp_x = exp(x);
  8.  
  9. //Можно использовать unsigned long long int.
  10. double sum = 1;
  11. int count = 1;
  12. int pow_x = 1;
  13. int fact = 1;
  14.  
  15. do {
  16. pow_x *= x;
  17. fact *= count;
  18. ++count;
  19. sum += pow_x / (double)fact;
  20. cout << count << " " << setprecision(9) << sum << endl;
  21. } while (sum <= exp_x - eps || sum >= exp_x + eps);
  22.  
  23. return count;
  24. }
  25.  
  26. int main() {
  27. cout << getCountSlag(1, 1e-7);
  28. return 0;
  29. }
Success #stdin #stdout 0s 4912KB
stdin
Standard input is empty
stdout
2 2
3 2.5
4 2.66666667
5 2.70833333
6 2.71666667
7 2.71805556
8 2.71825397
9 2.71827877
10 2.71828153
11 2.7182818
11