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\n",mid);
        else
            printf("No solution!\n");
    }
    return 0;
}

 

 
posted on 2018-08-27 11:44  蔡军帅  阅读(100)  评论(0编辑  收藏  举报