fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. double f(double x) {
  6. return x*x*x - x - 2;
  7. }
  8.  
  9. int main() {
  10. double a, b, c;
  11. double eps = 1e-6;
  12.  
  13. cin >> a >> b;
  14.  
  15. if (f(a) * f(b) >= 0) {
  16. cout << "Zly przedzial";
  17. return 0;
  18. }
  19.  
  20. while ((b - a) > eps) {
  21. c = (a + b) / 2;
  22. if (f(a) * f(c) < 0)
  23. b = c;
  24. else
  25. a = c;
  26. }
  27.  
  28. cout << (a + b) / 2;
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5304KB
stdin
1 2
stdout
1.52138