HDU-2199(二分)

Can you solve this equation?

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

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!
 
题意:给一个y值,求方程在0到100之间有没有解。
思路:二分。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 using namespace std;
 5 
 6 #define eps 0.000001
 7 
 8 int t;
 9 double y;
10 
11 double f(double x){
12     return 8.0*x*x*x*x+7.0*x*x*x+2.0*x*x+3.0*x+6.0;
13 }
14 
15 //递归版eps二分
16 double b_srch(double begin, double end){
17     double mid = (begin + end) / 2;
18     double ret;
19     double result = f(mid);
20     if(fabs(result - y) < eps){
21         return mid;
22     }
23     else if(result > y){
24         ret = b_srch(begin, mid);
25     }
26     else if(result < y){
27         ret = b_srch(mid, end);
28     }
29     return ret;
30 }
31 
32 //循环版eps二分
33 double b_srch2(double begin, double end){
34     double mid = (begin + end) / 2;
35     while(fabs(f(mid) - y) > eps){
36         if(f(mid) > y){
37             end = mid;
38         }
39         else{
40             begin = mid;
41         }
42         mid = (begin + end) / 2;
43     }
44     return mid;
45 }
46 
47 int main()
48 {
49     scanf("%d", &t);
50     while(t--){
51         scanf("%lf", &y);
52         if(y < 6 || y > 807020306){
53             printf("No solution!\n");
54         }
55         else{
56             double result = b_srch(0, 100);
57             printf("%.4f\n", result);
58         }
59     }
60     return 0;
61 }

 

posted @ 2016-02-22 13:40  喷水小火龙  阅读(130)  评论(0)    收藏  举报