hdu 2199 Can you solve this equation? 二分

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

#include<stdio.h>
#include<math.h>
#define eps 1.0e-10
double f(double x) {
return 8.0 * pow(x, 4.0) + 7.0 * pow(x, 3.0) + 2.0 * pow(x, 2.0) + 3 * x + 6;
}
void solve(double y) {
double left, right, mid;
left = 0.0, right = 100.0;
if (f(left) > y || f(right) < y) {
puts("No solution!");
} else {
while (fabs(left - right) > eps) {
mid = (left + right) / 2;
if (f(mid) > y) {
right = mid;
} else {
left = mid;
}
}
printf("%.4lf\n", mid);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int t;
double y;
scanf("%d", &t);
while (t--) {
scanf("%lf", &y);
solve(y);
}
return 0;
}

原文地址:https://www.cnblogs.com/xiaoxian1369/p/2204133.html