hdu 2899 二分查找

/*
题意:给出一个公式F(x),每给出一个y,求出F(x)的最小值

题解:二分查找
首先对F(x)求导,则当F'(x)=0时F(x)取最小值,因此求出F'(x)=0时的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 42*x*x*x*x*x*x+48*x*x*x*x*x+21*x*x+10*x;
}

double result1(double x, double y)
{
    return 6*x*x*x*x*x*x*x+8*x*x*x*x*x*x+7*x*x*x+5*x*x-y*x;
}

double bin(double front, double tail, double value)
{
    double mid = (front+tail)/2;
    
    while (!bingo(front, tail))
    {
        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);
        ans = result1(ans, y);
        printf("%.4lf\n",ans);
    }
    return 0;
}

 

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