二分-D - Can you solve this equation?

D - Can you solve this equation?

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.

InputThe 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);OutputFor 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!

 

 

 

 

 

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 
 5 double f(double x){
 6     return (8*pow(x,4) + 7*pow(x,3) + 2*pow(x,2) + 3*x + 6);
 7 }
 8 
 9 int main()
10 {
11     int t;
12     double m,n;
13     scanf("%d",&t);
14     while(t--){
15         scanf("%lf",&m);
16         if(f(0)>m||f(100)<m){
17             printf("No solution!\n");
18             continue;
19         }
20         double left = 0, right = 100, mid;
21         while(fabs(f(mid)-m) > 1e-5){
22             mid=(left + right)/2;
23             if((f(mid) > m))     right = mid;    
24              else if(f(mid)<m)     left=mid;
25         }
26         printf("%.4lf\n",mid);
27     }
28     return 0;
29 }

Guess you like

Origin www.cnblogs.com/0424lrn/p/12228023.html