fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double f(double x) {
  7. return (x-2)*(x-2)+5;
  8. }
  9.  
  10. double f1(double x) {
  11. return 2*(x-2);
  12. }
  13.  
  14. int main() {
  15. double a, b, eps, c, xmin;
  16.  
  17.  
  18. cin >> a >> b >> eps;
  19.  
  20. int k = 0;
  21.  
  22. while (abs(b - a) > eps) {
  23. c = (b + a) / 2;
  24.  
  25.  
  26. if (f1(c) == 0) {
  27. cout << "exact min " << c;
  28. return 0;
  29. }
  30.  
  31. if (f1(c) > 0) {
  32. b = c;
  33. }
  34.  
  35. if (f1(c) < 0) {
  36. a = c;
  37. }
  38.  
  39. k++;
  40. }
  41.  
  42. xmin = (b + a) / 2;
  43. cout << xmin << " " << k;
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0.01s 5280KB
stdin
0 4 0.1
stdout
exact min 2