(二分搜索)Can you solve this equation? -- hdu -- 2199

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2199

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


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!

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <queue>
#include <algorithm>
using namespace std;

const int oo = 0x3f3f3f3f;
const int N = 222222;



int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int flag=0;
        double y, l=0, r=100, x;
        scanf("%lf", &y);

        double m = 8*100*100*100*100 + 21*100*100*100 + 4*100*100 + 3*100 + 6;

        if(fabs(y-m)<0.0001)
        {
            printf("100.0000
");
            continue;
        }

        while(l<r)
        {
            x = (l+r)/2;
            m = 8*x*x*x*x + 7*x*x*x + 2*x*x + 3*x + 6;

            if(fabs(m-y)<0.0001)
            {
                flag = 1;
                break;
            }
            else if(m>y)
                r = x;
            else if(m<y)
                l = x;
        }

        if(flag)
        printf("%.4f
", x);
        else
        printf("No solution!
");
    }
    return 0;
}
勿忘初心
原文地址:https://www.cnblogs.com/YY56/p/4783093.html