HDU 2199 Can you solve this equation?_二分搜索

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #define sc(x) scanf("%d",&(x))
 5 #define pf(x) printf("%.4lf
", x)
 6 using namespace std;
 7 int T;
 8 double Y;
 9 double r(double x)
10 {
11     return (8*pow(x, 4) + 7*pow(x, 3) + 2*pow(x, 2) + 3*x + 6);
12 }
13 double Find()
14 {
15     double front = 0, rear = 100, mid;
16     while((rear - front) > 1e-6)
17     {
18         mid = (front + rear) / 2;
19         if(r(mid) < Y)
20             front = mid + 1e-7;
21         else
22             rear = mid - 1e-7;
23     }
24     return (front + rear) / 2;
25 }
26 int main()
27 {
28     sc(T);
29     while(T--)
30     {
31         cin >> Y;
32         if(r(0) <= Y && Y <= r(100))
33             pf(Find());
34         else
35             cout << "No solution!" << endl;
36     }
37     return 0;
38 }
View Code
原文地址:https://www.cnblogs.com/ghostTao/p/4305950.html