三分查找,模板题
参考 https://blog.csdn.net/beiyouyu/article/details/7875480
本题要点:
1、对于x进行三分查找。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
const double eps = 1e-7;
int y;double f(double x)
{return 6 * pow(x, 7) + 8 * pow(x, 6) + 7 * pow(x, 3) + 5 * pow(x, 2) - y * x;
}double three_search()
{double l = 0, r = 100, lm, rm; while(l + eps < r){lm = (l + r) / 2, rm = (lm + r) / 2;if(f(lm) > f(rm)) // 右部分区间 [rm, r] 更小,舍弃左部分区间 [l, lm]{l = lm;}else{r = rm;}}return f(l);
}int main()
{int T;scanf("%d", &T);while(T--){scanf("%d", &y);printf("%.4lf\n", three_search());}return 0;
}/* 2 100 200 *//* -74.4291 -178.8534 */