欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-03-17 15:22:34
...

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<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
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;
}