fork download
  1. #include <iostream>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. const double eps = 0.01;
  7. double a, b, t, x, y, t1, s;
  8.  
  9. double func(double a, double b, double e=0)
  10. {
  11. s = (a + b) / 2;
  12. y = 7 * sin(2*(s+e));
  13. return y;
  14. }
  15.  
  16. void JustDoIt(double &a,double &b)
  17. {
  18. t = (a + b) / 2.0;
  19. y = func(a, b, - eps);
  20. x = func(a, b, eps);
  21. if (y <= x) a = t;
  22. else b = t;
  23. // чек-блок
  24. cout << "t= " << t <<"\n";
  25. cout << "a= " << a <<" b=" << b <<"\n";
  26. cout << "x= " << x <<" y=" << y <<"\n";
  27. cout << "fabs(b - a)= " << fabs(b - a) <<"\n";
  28. //
  29. if ( fabs(b - a) < eps) return;
  30. JustDoIt( a, b);
  31. }
  32.  
  33. int main()
  34. {
  35. a = 2;
  36. b = 6;
  37. JustDoIt( a, b);
  38. cout << "Otvet: " << func(a, b);
  39. return 0;
  40. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
t= 4
a= 4 b=6
x= 6.90375 y=6.90375
fabs(b - a)= 2
t= 5
a= 5 b=6
x= -3.92485 y=-3.92485
fabs(b - a)= 1
t= 5.5
a= 5.5 b=6
x= -6.99791 y=-6.99791
fabs(b - a)= 0.5
t= 5.75
a= 5.75 b=6
x= -6.05928 y=-6.05928
fabs(b - a)= 0.25
t= 5.875
a= 5.875 b=6
x= -5.00376 y=-5.00376
fabs(b - a)= 0.125
t= 5.9375
a= 5.9375 b=6
x= -4.35442 y=-4.35442
fabs(b - a)= 0.0625
t= 5.96875
a= 5.96875 b=6
x= -4.00359 y=-4.00359
fabs(b - a)= 0.03125
t= 5.98438
a= 5.98438 b=6
x= -3.82222 y=-3.82222
fabs(b - a)= 0.015625
t= 5.99219
a= 5.99219 b=6
x= -3.73013 y=-3.73013
fabs(b - a)= 0.0078125
Otvet: -3.80204