首页 > 代码库 > hdu 2199 二分搜索

hdu 2199 二分搜索

Can you solve this equation?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19647    Accepted Submission(s): 8664


Problem Description
Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky.
 

 

Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);
 

 

Output
For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.
 

 

Sample Input
2 100 -4
 

 

Sample Output
1.6152 No solution!

 

注意控制精度,不然会超时,虽然是保留四位小数,但是精度得10的-8次方左右

 1 #include<iostream>
 2 #include<stdio.h>
 3 #define exp 1e-8
 4 using namespace std;
 5 
 6 double key;
 7 
 8 double Binary(double low,double high)
 9 {
10     while(high-low>exp)
11     {
12         double mid = (low+high)/2;
13         if(8*mid*mid*mid*mid + 7*mid*mid*mid + 2*mid*mid + 3*mid + 6 > key)
14             high=mid-exp;
15         else
16             low=mid+exp;
17     }
18     return low;
19 }
20 int main()
21 {
22     int t;
23     scanf("%d",&t);
24     while(t--)
25     {
26         scanf("%lf",&key);
27         if(6>key || 8*100*100*100*100 + 7*100*100*100 + 2*100*100 + 3*100 + 6 < key)
28             printf("No solution!\n");
29         else
30             printf("%.4f\n",Binary(0, 100));
31     }
32     return 0;
33 }

 

hdu 2199 二分搜索