fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double f(double x)
  7. {
  8. return x*x*x - x - 2; // mozna zmienic funkcje
  9. }
  10.  
  11. int main()
  12. {
  13. double a, b, srodek;
  14. double eps = 0.000001;
  15.  
  16. cin >> a >> b;
  17.  
  18. if (f(a) * f(b) >= 0)
  19. return 0;
  20.  
  21. while ((b - a) / 2 > eps)
  22. {
  23. srodek = (a + b) / 2;
  24.  
  25. if (f(a) * f(srodek) < 0)
  26. b = srodek;
  27. else
  28. a = srodek;
  29. }
  30.  
  31. srodek = (a + b) / 2;
  32. cout << srodek;
  33.  
  34. return 0;
  35. }
  36.  
  37.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty