hdu 2899 Strange fuction 二分

/*
* hdu2899.c
*
* Created on: 2011-10-9
* Author: bjfuwangzhu
*/

#include<stdio.h>
#include<math.h>
#define eps 1.0e-8
double ff(double x, double y) {
return 42.0 * pow(x, 6.0) + 48.0 * pow(x, 5.0) + 21.0 * pow(x, 2.0)
+ 10.0 * x - y;
}
double f(double x, double y) {
return 6.0 * pow(x, 7.0) + 8.0 * pow(x, 6.0) + 7.0 * pow(x, 3.0)
+ 5.0 * pow(x, 2.0) - y * x;
}
double bfsearch(double y) {
double left, right, mid;
left = 0.0, right = 100.0;
while (fabs(left - right) > eps) {
mid = (left + right) / 2.0;
if (ff(mid, y) < 0.0) {
left = mid;
} else {
right = mid;
}
}
return mid;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int t;
double y, x;
scanf("%d", &t);
while (t--) {
scanf("%lf", &y);
x = bfsearch(y);
printf("%.4lf\n", f(x, y));
}
return 0;
}
原文地址:https://www.cnblogs.com/xiaoxian1369/p/2203667.html