fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // Funkcja
  6. double f(double x)
  7. {
  8. return x * x * x - x - 2;
  9. }
  10.  
  11. int main()
  12. {
  13. double a, b, eps, srodek;
  14.  
  15. // Odczyt danych ze standardowego wejścia
  16. cin >> a >> b >> eps;
  17.  
  18. // Sprawdzenie warunku zmiany znaku
  19. if (f(a) * f(b) >= 0)
  20. {
  21. return 0;
  22. }
  23.  
  24. // Metoda bisekcji
  25. while ((b - a) > eps)
  26. {
  27. srodek = (a + b) / 2;
  28.  
  29. if (f(srodek) == 0)
  30. break;
  31. else if (f(a) * f(srodek) < 0)
  32. b = srodek;
  33. else
  34. a = srodek;
  35. }
  36.  
  37. srodek = (a + b) / 2;
  38.  
  39. // Wynik
  40. cout << srodek << endl;
  41.  
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty