HDU 2199 Can you solve this equation?(二分)

Can you solve this equation?

Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 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!

题意:
题意很简单,就是让你求在0~100的实数范围内方程是否有解
思路:
先判断是否有解,在有解的情况下进行二分,二分的时候考虑一下精度,精度的话我是到1e-8过了

#include
#include
#include
#include
#include
#include
using namespace std;
const int inf=0x3f3f3f3f;
double solve(double x)
{
    return 8*pow(x,4.0)+7*pow(x,3.0)+2*pow(x,2.0)+3*x+6;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        double y;
        scanf("%lf",&y);
        if(y<solve(0.0)||y>solve(100.0)) //无解
        {
            printf("No solution!\n");
        }
        else
        {
            double l=0,r=100,mid,ans;
            while(r-l>1e-8)
            {
                mid=(l+r)/2;
                ans=solve(mid);
                if(ans>y)
                {
                    r=mid;
                }
                else
                {
                    l=mid;
                }
            }
            printf("%.4lf\n",mid);
        }
    }
    return 0;
}

你可能感兴趣的:(#,【尺取,二分,三分】)