Advertisement
allia

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

Oct 26th, 2020 (edited)
2,553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  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.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement