2021-11-13-HDOJ-2899

2021-11-13  本文已影响0人  termanary

三分模板题:
坑人的是,精度设置为1e-4是不行的,只是要求输出小数点后四位而已,精度得继续调才行:

#include<cstdio>

// using namespace std;

const double eps = 1e-8;

double fx(double x,double y)
{
    double x1,x2,x3,x6,x7;
    x1 = x;
    x2 = x1 * x1;
    x3 = x2 * x1;
    x6 = x3 * x3;
    x7 = x6 * x1;
    x7 *= 6;
    x6 *= 8;
    x3 *= 7;
    x2 *= 5;
    x1 *= -y;
    return x1+x2+x3+x6+x7;
}

// double fx2(double x,double y)
// {
//     double x1,x2,x3,x6,x7;
//     x1 = x * (-y);
//     x2 = x * x * 5;
//     x3 = x * x * x * 7;
//     x6 = x * x * x * x * x * x * 8;
//     x7 = x * x * x * x * x * x * x * 6;
//     return x1+x2+x3+x6+x7;
// }

double cal(double y)
{
    double lborder,rborder;
    double lmin,rmin;
    double valuelmin,valuermin;
    for(lborder=0,rborder=100;;)
    {
        lmin=(rborder-lborder)/3+(lborder);
        rmin=(rborder-lborder)/3*2+(lborder);
        valuelmin = fx(lmin,y);
        valuermin = fx(rmin,y);
        // printf("%lf %lf %lf %lf %lf %lf \n",lborder,rborder,lmin,rmin,valuelmin,valuermin);
        if(valuelmin < valuermin)
        {
            rborder = rmin;
            if(valuermin - valuelmin < eps)
            {
                // printf("%lf %lf %lf %lf %lf %lf \n",lborder,rborder,lmin,rmin,valuelmin,valuermin);
                return valuelmin;
            }
        }
        else
        {
            lborder = lmin;
            if(valuelmin - valuermin < eps )
            {
                // printf("%lf %lf %lf %lf %lf %lf \n",lborder,rborder,lmin,rmin,valuelmin,valuermin);
                return valuermin;
            }
        }
    }
    return 0;
}

int main(void)
{
    int t,y;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&y);
        printf("%.4lf\n",cal(y));
    }
    return 0;
}

上一篇下一篇

猜你喜欢

热点阅读