HDOJ 2899 二分搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2899
解题思路:F(x)求导,二分搜索F'(x)=0时的x的值。
#include <iostream>
#include <cmath>
using namespace std;
const double eps = 1e-12;
double bsearch(double y)
{
double mid, tmp;
double left = 0, right = 100;
while (fabs(right - left) > eps)
{
mid = (left + right) / 2;
tmp = 42 * pow(mid, 6) + 48 * pow(mid, 5) + 21 * pow(mid, 2) + 10 * mid;
if (fabs(tmp - y) <= eps) return mid;
if (tmp < y) left = mid + eps;
else right = mid - eps;
}
return (left + right) / 2;
}
int main()
{
int t;
double x, y;
for (scanf("%d", &t); t--; )
{
scanf("%lf", &y);
x = bsearch(y);
printf("%.4lf\n", 6 * pow(x, 7) + 8 * pow(x, 6) + 7 * pow(x, 3) + 5 * pow(x, 2) - x * y);
}
}