fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. double f(double x){
  6. return x*x-6*x-8;
  7. }
  8. int main() {
  9.  
  10. double a,b,eps;
  11. cin>>a>>b;
  12. eps=0.00001;
  13. int k=0;
  14. while(abs(b-a)>=eps){
  15. double xm,x1,x2;
  16. xm=(a+b)/2;
  17. x1=xm-(eps/4);
  18. x2=xm+(eps/4);
  19. if (f(x1)>f(x2)){
  20. a=x1;
  21.  
  22. }
  23. if(f(x1)<=f(x2)){
  24. b=x2;
  25. }
  26. k=k+1;
  27. cout<<a<<" "<<b<<endl;
  28.  
  29. }
  30. cout<<(a+b)/2<<" "<<k;
  31.  
  32. }
Success #stdin #stdout 0s 5320KB
stdin
1 5
stdout
1 3
2 3
2.5 3
2.75 3
2.875 3
2.9375 3
2.96875 3
2.98437 3
2.99219 3
2.99609 3
2.99804 3
2.99902 3
2.99951 3
2.99975 3
2.99988 3
2.99994 3
2.99997 3
2.99998 3
2.99999 3
2.99999 3
3 20