• BestCoder Round #18(hdu5105)Math Problem(高中数学)


    最大值无非就是在两个端点或极值点处取得。

    我注意讨论了a=0和b=0,却忽略了极值点可能不在L到R的范围内这一问题。被Hack了。

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #include<cctype>
    #include<sstream>
    using namespace std;
    #define pii pair<int,int>
    #define LL long long int
    const int eps=1e-8;
    const int INF=1000000000;
    const int maxn=10000+10;
    double a,b,c,d,L,R,ans;
    double f(double x)
    {
        return fabs(a*x*x*x+b*x*x+c*x+d);
    }
    int main()
    {
        //freopen("in10.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&L,&R)==6)
        {
            if(a!=0)
            {
                if(a<0)
                {
                    a=-a;
                    b=-b;
                    c=-c;
                    d=-d;
                }
                double pan=b*b-3*a*c;
                if(pan<=0)
                {
                    printf("%.2f
    ",max(f(L),f(R)));
                }
                else
                {
                    double x1=(-sqrt(pan)-b)/(3*a);
                    double x2=(sqrt(pan)-b)/(3*a);
                    if(x1>=L&&x1<=R&&x2>=L&&x2<=R)
                    printf("%.2f
    ",max(max(f(L),f(R)),max(f(x1),f(x2))));
                    else if(x1>=L&&x1<=R)
                        printf("%.2f
    ",max(f(L),max(f(R),f(x1))));
                    else if(x2>=L&&x2<=R)
                        printf("%.2f
    ",max(f(L),max(f(R),f(x2))));
                    else printf("%.2f
    ",max(f(L),f(R)));
                }
            }
            else
            {
                if(b==0) printf("%.2f
    ",max(f(L),f(R)));
                else
                {
                    double z=(-c)/(2*b);
                    if(z<=R&&z>=L)
                    printf("%.2f
    ",max(f(L),max(f(R),f(z))));
                    else printf("%.2f
    ",max(f(L),f(R)));
                }
            }
        }
        //fclose(stdin);
        //fclose(stdout);
        return 0;
    }
  • 相关阅读:
    SpringMVC在Controller层中注入request的坑
    org.postgresql.util.PSQLException: 栏位索引超过许可范围:3,栏位数:2。
    jQuery函数attr()和prop()的区别
    jquery中prop()方法和attr()方法的区别浅析
    Android View 绘制流程
    Android配置构建变体
    使用 Java 8 语言功能
    HTTPS和HTTP的区别
    “京东金融”主页效果 RecyclerView联动
    javasscript基础
  • 原文地址:https://www.cnblogs.com/zywscq/p/4100776.html
Copyright © 2020-2023  润新知