allia

бинарный поиск

Oct 26th, 2020 (edited)
1,224
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 funcction (int a, int b, int c, int d, double x)
  8. {
  9.   double y;
  10.   y = a*x*x*x + b*x*x + c*x + d;
  11.   return y;
  12. }
  13.  
  14. int main()
  15. {
  16.   int a, b, c, d;
  17.   double eps = 0.000001;
  18.  
  19.   cin >> a >> b >> c >> d;
  20.  
  21.   double first = -10000;
  22.   double last = 10000;
  23.  
  24.   while (last - first > eps)
  25.   {
  26.     double middle = (last + first)/2;
  27.    
  28.     if (funcction(a, b, c, d, middle)*funcction(a, b, c, d, last) > 0)
  29.       last = middle;
  30.     else first = middle;
  31.   }
  32.   cout.precision(6);
  33.  
  34.   cout << fixed << first;
  35.  }
RAW Paste Data