HDU 2199 Can you solve this equation?

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2199

二分,注意精度就好

#include <iostream>
#include <cmath>
using namespace std;
double Y;
double cal(double x)
{
    return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6;
}
double BinarySearch(double low,double heigh)
{
    if(low>=heigh)
        return 0;
    double mid = (low+heigh)/2;
    //    printf("%lf中值%lf下界%lf上界%lf\n",mid,res(mid),low,heigh);

    if(abs(cal(mid)-Y)<=0.00001)
        return mid;
    if(cal(mid)<Y)
        return BinarySearch(mid,heigh);
    else 
        return BinarySearch(low,mid);

}
int main(int argc, const char *argv[])
{
    int n;

    cin>>n;
    while(n--)
    {
        cin>>Y;
        if(cal(0)>Y||cal(100)<Y)
        {
            printf("No solution!\n");
        }
        else
        {
            double rs = BinarySearch(0,100);
            printf("%.4lf\n",rs);
        }

    }
    return 0;
}

 

posted @ 2013-09-22 17:11  Destino74  阅读(153)  评论(0编辑  收藏  举报