uzimane_

тернарный поиск

Jul 10th, 2020
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int vp, vf;
  8. double r = 1, l = 0;
  9. double sep;
  10. double eps;
  11. double x;
  12.  
  13. double fun(double x)
  14. {
  15. return ((sqrt(pow(1-sep, 2) + pow(x, 2)) / vp) + (sqrt(pow(sep, 2) + pow(1-x, 2)) / vf));
  16. }
  17.  
  18. double tsearch(double l , double r, double eps)
  19. {
  20.  
  21. if ((r - l) < eps)
  22. return (l + r) / 2;
  23. double a = (2 * l + r) / 3;
  24. double b = (l + 2 * r) / 3;
  25. if (fun(b) > fun(a))
  26. return tsearch(l, b, eps);
  27. else
  28. return tsearch(a, r, eps);
  29. }
  30.  
  31. int main()
  32. {
  33.  
  34. freopen("input.txt", "r", stdin);
  35. freopen("output.txt", "w", stdout);
  36.  
  37. cin >> vp >> vf;
  38. cin >> sep;
  39. eps = pow(10, -12);
  40.  
  41. printf("%.10lf\n", tsearch(l, r, eps));
  42. //cout << tsearch(l, r, eps);
  43.  
  44. }
Add Comment
Please, Sign In to add comment