Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include<iostream>
- #include<string>
- #include<alg.h>
- using namespace std;
- const double EPS = 1e-4;
- const int INF = 1e8 + 13;
- double func(double x)
- {
- return 2. * x*x - 6 * x - 3;
- }
- pair<double, double> solve()
- {
- double left = -1;
- double right = 3;
- double step = 0.1;
- double answer = INF;
- double answerPoint = -INF;
- while (step >= EPS)
- {
- step /= 2;
- for (double i = left; i <= right; i += step)
- {
- double cur = func(i);
- if (answer == INF)
- {
- answer = cur;
- continue;
- }
- if (cur <= answer)
- {
- answer = cur;
- answerPoint = i;
- }
- else
- {
- right = min(i + step, right);
- break;
- }
- }
- step /= 2;
- for (double i = right; i >= left; i -= step)
- {
- double cur = func(i);
- if (cur <= answer)
- {
- answer = cur;
- answerPoint = i;
- }
- else
- {
- left = max(i - step, left);
- break;
- }
- }
- }
- return make_pair(answer, answerPoint);
- }
- int main()
- {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- pair<double, double> ans = solve();
- cout << "min = " << ans.first << "\nwith x = " << ans.second << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement