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-3;
- const double DELTA = 1e-5;
- 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 (right - left > EPS)
- {
- double mid = (left + right) / 2;
- double F1 = func(mid - DELTA);
- double F2 = func(mid + DELTA);
- if (F1 > F2)
- left = mid;
- else
- if (F2 > F1)
- right = mid;
- if (F1 == F2)
- return { F1, mid };
- }
- answer = func((left + right) / 2);
- answerPoint = (left + right) / 2;
- 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