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