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

Posted caiyishuai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2199 Can you solve this equation?(二分精度)相关的知识,希望对你有一定的参考价值。

HDU 2199 Can you solve this equation?
 
 
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.

InputThe 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);OutputFor 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!

这种题一般都很注重精度的,可以拿两个端点来做验证,6的时候应该为0.0000,807020306的时候应该为100.0000
其中while(ri-le<=0.000000001)这里的0尽量多一点,而判断的时候的0的个数要看情况,有些当满足
ri-le<=0.000000001时,说不定判断还不满足。

#include <iostream>
#include <stack>
#include <string.h>
#include <stdio.h>
#include<queue>
#include<algorithm>
#define ll long long
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        double y;
        cin>>y;
        double le,ri,mid;
        le=0;ri=100;
        bool f=0;
        while(ri-le>=0.00000000000001)
        {
            mid=(le+ri)/2;
            //cout<<8*mid*mid*mid*mid+7*mid*mid*mid+2*mid*mid+3*mid+6-y<<endl;
            if(8*mid*mid*mid*mid+7*mid*mid*mid+2*mid*mid+3*mid+6-y<0.00001&&8*mid*mid*mid*mid+7*mid*mid*mid+2*mid*mid+3*mid+6-y>=0)
            {
                f=1;
                break;
            }
            else if(8*mid*mid*mid*mid+7*mid*mid*mid+2*mid*mid+3*mid+6-y<0)
            {
                le=mid;
            }
            else
                ri=mid;
        }
        if(f)
            printf("%.4lf
",mid);
        else
            printf("No solution!
");
    }
    return 0;
}

 

 






以上是关于HDU 2199 Can you solve this equation?(二分精度)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2199 Can you solve this equation?

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

hdu 2199Can you solve this equation?(牛顿迭代法)

杭电2199.Can you solve this equation?

HDU 1086 [You can Solve a Geometry Problem too] 计算几何

HDOJ1086-You can Solve a Geometry Problem too(线段相交)