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

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

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

Can you solve this equation?

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description
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]找个数x满足题中方程
二分x即可

#include<bits/stdc++.h>
using namespace std;

double F(double x)
{
    return 8*pow(x,4)+7*pow(x,3)+2*x*x+3*x+6;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        double y;
        scanf("%lf", &y);
        if(y<6||y>807020306)//x=0或100带入为方程最小最大值,范围外的直接Pass
        {
            printf("No solution!\n");
            continue;
        }
        double l=0, r=100, m;
        while(r-l>1e-6)//因为找不到准确的某个数,所以找个近似值,精确到1e-6即可
        {
            m=(l+r)/2.0;
            if(F(m)>y)//说明x取大了,m右边的都不能要,r=m
                r=m;
            else//反之亦然
                l=m;
        }
        printf("%.4f\n", m);
    }
    return 0;
}