fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double fpokh(double x) {
  7. return 2*x-6;
  8. }
  9.  
  10. int main() {
  11.  
  12. std::ios_base::sync_with_stdio(false);
  13. std::cin.tie(NULL);
  14.  
  15. double a = 1;
  16. double b = 5;
  17. double c;
  18. double e = 0.001;
  19. int k = 0;
  20.  
  21.  
  22. while (abs(b - a) > e) {
  23. c = (a + b) / 2.0;
  24. k++;
  25.  
  26. double der = fpokh(c);
  27.  
  28. if (der == 0.0) {
  29. a = c;
  30. b = c;
  31. break;
  32. } else if (der > 0) {
  33. b = c;
  34. } else {
  35. a = c;
  36. }
  37. }
  38.  
  39. double xmin = (a + b) / 2.0;
  40. cout << xmin << " " << k << '\n';
  41.  
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
3 1