allia

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

Oct 27th, 2020
951
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. double function (double v1, double v2, double a, double x)
  8. {
  9.   double time;
  10.   time = (sqrt((1-a)*(1-a) + x*x))/v1 + (sqrt((1-x)*(1-x) + a*a))/v2;
  11.   return time;
  12. }
  13.  
  14. double poisk (double v1, double v2, double a, double left, double right)
  15. {
  16.  double m1, m2;
  17.   while (right - left > 0.00000001)
  18.   {
  19.      m1 = left + (right - left)/3;
  20.      m2 = right - (right - left)/3;
  21.  
  22.     double znach1 = function (v1, v2, a, m1);
  23.     double znach2 = function (v1, v2, a, m2);
  24.     //cout << znach1 << " " << znach2 << endl;
  25.     if (znach1 > znach2)
  26.      left = m1;
  27.     else if (znach1 < znach2)
  28.      right = m2;
  29.      else if (znach1 == znach2)
  30.       {
  31.         left = m1;
  32.         right = m2;
  33.       }
  34.   }
  35.   //cout << m1 << " ";
  36.   return left;
  37. }
  38.  
  39. int main ()
  40. {
  41.   double speed_1, speed_2, granica;
  42.  
  43.   cin >> speed_1 >> speed_2;
  44.   cin >> granica;
  45.  
  46.   cout.setf(ios::fixed);  // вывод в фиксированном формате
  47.   cout.precision(8);
  48.  
  49.   cout << poisk (speed_1, speed_2, granica, 0, 1);
  50. }
RAW Paste Data