fork download
  1. #include <iostream>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5. double f(double x)
  6. {
  7. return log(1 + x*x - sin(x)) - pow(3, cos(2*x));
  8. }
  9.  
  10. double findRoot(double a, double b, double e)
  11. {
  12. while (b - a > e)
  13. {
  14. if (f(a) * f((b + a) / 2) == 0)
  15. break;
  16. else if (f(a) * f((b + a) / 2) > 0)
  17. a = (b + a) / 2;
  18. else
  19. b = (b + a) / 2;
  20. }
  21. return (b + a) / 2;
  22. }
  23.  
  24. int main()
  25. {
  26. double e;
  27. cin >> e;
  28. cout << findRoot(-4, -3, e) << endl;
  29. cout << findRoot(-3, -2, e) << endl;
  30. cout << findRoot(-1, 0, e) << endl;
  31. cout << findRoot(1, 2, e) << endl;
  32. cout << findRoot(2, 3, e) << endl;
  33. cout << findRoot(3, 4, e) << endl;
  34. return 0;
  35. }
Success #stdin #stdout 0s 3464KB
stdin
0.00001
stdout
-3.42634
-2.75443
-0.836887
1.2171
2.72062
3.41392