Advertisement
Infiniti_Inter

поразряд

Oct 7th, 2020 (edited)
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<iostream>
  3. #include<string>
  4. #include<alg.h>
  5.  
  6. using namespace std;
  7. const double EPS = 1e-4;
  8. const int INF = 1e8 + 13;
  9.  
  10.  
  11.  
  12. double func(double x)
  13. {
  14.     return 2. * x*x - 6 * x - 3;
  15. }
  16.  
  17. pair<double, double> solve()
  18. {
  19.  
  20.     double left = -1;
  21.     double right = 3;
  22.     double step = 0.1;
  23.     double answer = INF;
  24.     double answerPoint = -INF;
  25.     while (step >= EPS)
  26.     {
  27.         step /= 2;
  28.         for (double i = left; i <= right; i += step)
  29.         {
  30.             double cur = func(i);
  31.             if (answer == INF)
  32.             {
  33.                 answer = cur;
  34.                 continue;
  35.             }
  36.             if (cur <= answer)
  37.             {
  38.                 answer = cur;
  39.                 answerPoint = i;
  40.             }
  41.             else
  42.             {
  43.                 right = min(i + step, right);
  44.                 break;
  45.             }
  46.         }
  47.         step /= 2;
  48.         for (double i = right; i >= left; i -= step)
  49.         {
  50.             double cur = func(i);
  51.             if (cur <= answer)
  52.             {
  53.                 answer = cur;
  54.                 answerPoint = i;
  55.             }
  56.             else
  57.             {
  58.                 left = max(i - step, left);
  59.                 break;
  60.             }
  61.         }
  62.     }
  63.     return make_pair(answer, answerPoint);
  64. }
  65.  
  66. int main()
  67. {
  68.     freopen("input.txt", "r", stdin);
  69.     freopen("output.txt", "w", stdout);
  70.  
  71.     pair<double, double> ans = solve();
  72.     cout << "min = " << ans.first << "\nwith x =  " << ans.second << endl;
  73.  
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement