HDOJ Can you solve this equation? 2199【二分検索】

4353 ワード

Can you solve this equation?
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12697    Accepted Submission(s): 5664
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!

 
Author
Redow
 
Recommend
lcy   |   We have carefully selected several similar problems for you:   2899  2289  2298  3400  1969 
 
Statistic |  Submit |  Discuss | Note
二分ルックアップxの値注意精度No solutionの場合x<0||x>100の場合はYの値をそのまま先に判断すればよい
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

using namespace std;

int find_x(double x,double Y)
{
    if( Y-(8.0*pow(x,4.0) + 7.0*pow(x,3.0) + 2.0*pow(x,2.0) + 3.0*x + 6.0) < 1e-10) return 1;
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        int flag=0;
        double Y;
        scanf("%lf",&Y);
        double L,R,mid;
        L=0;
        R=100;
        if(Y<6||Y>807020306){
            printf("No solution!
"
); continue; } while(R-L>1e-8){ mid=(L+R)/2.0; if(find_x(mid,Y)==1){ R=mid; } else if(find_x(mid,Y)==0){ L=mid; } } printf("%.4lf
"
,mid); } return 0; }