HDU 2199 Can you solve this equation? 二分

Can you solve this equation?
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

现在,给出等式8* X^4+ 7* X^3+ 2* X^2+ 3 * X +6= Y,请找出他在0和100之间的解(包含0和100)。
现在,请你试试运气。。。。
 

Input

输入的第一行包含一个整数T(1 <= T <=100),表示测试用例的数目。接下来T个数字,每一行都有一个实数Y(abs(Y)<=10^10);

Output

对于每个测试用例,如果有解,你应该输出一个实数(精确到小数点后4位,四舍五入),如果在0到100之间无解,就输出“No solution!”。

Sample Input

2

100

-4 

Sample Output

1.6152

No solution!

 

 

二分水题

#include <iostream>
#include <cstdio>
#include <math.h>
#include <cstring>
#include <algorithm>
#include <iomanip>

using namespace std;

//8* X^4+ 7* X^3+ 2* X^2+ 3 * X +6= Y
double fx(double x)
{
    return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6;
}


int main()
{
    //freopen("input.txt","r",stdin);
    int t;
    double y;
    cin>>t;
    while(t--)
    {
        cin>>y;
        double l=0;
        double r=100;
        double mid;
        int flag=0;

        if(y>=6&&fx(100)>=y)
        {
            while(r-l>1e-8)
            {
                mid=(r+l)/2;


                if(fx(mid)<y)
                {
                    l=mid;
                }
                else
                {
                    r=mid;
                }

            }
            cout<<fixed<<setprecision(4)<<l<<endl;
        }
        else
            cout<<"No solution!"<<endl;
    }
}

  

posted @ 2016-07-26 23:39  Hyouka  阅读(151)  评论(0编辑  收藏  举报