hdu 2199 二分查找

/*
题意:给出一个等式,和y的值,x为0~100之间的值,求出x(精确到4位小数)

题解:二分查找对x进行暴力搜索

注意:精度的大小要注意,否则会超时
*/#include <cstdio>

bool bingo(double left, double right)
{
    if (left > right)
        return true;
    int tmpl = left*10000,tmpr = right*10000;
    if (left == right)
        return true;
    else
        return false;
}

double result(double x)
{
    return 8*x*x*x*x+7*x*x*x+2*x*x+3*x+6;
}

double bin(double front, double tail, double value)
{
    if (value < 6 || value > 807020306)
        return -1;
    double mid = (front+tail)/2;
    
    while (!bingo(front, tail))
    {// 此处精度过大会导致超时,因为当x只有4为精度,因此y同样只可能保证4位精度
        if (-1e-5 < result(mid) - value && result(mid) - value < 1e-5)
            return mid;
        else if (result(mid) < value)
            front = mid;
        else
            tail = mid;
        mid = (front+tail)/2;
    }
    return mid;
}

int main(void)
{
    double y;
    int t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%lf",&y);
        double ans = bin(0,100,y);
        if (ans < 0)
            printf("No solution!\n");
        else
            printf("%.4lf\n",ans);
    }
    return 0;
}

 

posted @ 2014-03-20 23:31  辛力啤  阅读(636)  评论(0编辑  收藏  举报