HDU5105 Math Problem(数学题)

来源:互联网 发布:tl494引脚功能和数据 编辑:程序博客网 时间:2024/04/28 01:07

Math Problem 

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5105

题思路:

BestCoder官方题解:

f(x)=|ax3+bx2+cx+d|,求最大值。令g(x)=ax3+bx2+cx+d,f(x)的最大值即为g(x)的正最大值,或者是负最小值。a!=0

时,g(x)=3ax2+2bx+c求出g(x)的根(若存在,x1,x2,由导数的性质知零点处有极值。ans=max(f(xi)|LxiR).然后考虑两个点的特殊性有ans=max(ans,f(L),f(R)).

简而言之,就是取极值和端点值,然后考虑一下a=0时的情况还有b=0时的情况,还有就是要注意到极值点是否在区间[L,R]里面;即

可求出结果。

AC代码:

#include <iostream>#include <cstdio>#include <cmath>using namespace std;double a,b,c,d;double fun(double x){    return fabs(a*x*x*x+b*x*x+c*x+d);}int main(){    double l,r;    while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&l,&r)!=EOF){        double t = 4*b*b-12*a*c,maxn;        double f3 = fun(l),f4 = fun(r);        maxn = max(f3,f4);        if(t < 0)            printf("%.2lf\n",maxn);        else if(a == 0){            if(b == 0)                printf("%.2lf\n",maxn);            else{                double x = -c/(2*b);                if(x <= l || x >= r)                    printf("%.2lf\n",maxn);                else{                    double f = fun(x);                    printf("%.2lf\n",max(maxn,f));                }            }        }        else if(t == 0){            double x = -b/(2*a);            if(x <= l || x >= r)                printf("%.2lf\n",maxn);            else{                double f = fun(x);                printf("%.2lf\n",max(maxn,f));            }        }        else{            double x1 = (-2*b+sqrt(t))/(6*a),x2 = (-2*b-sqrt(t))/(6*a);            double f1 = fun(x1),f2 = fun(x2);            if((x1 <= l || x1 >= r) && (x2 >= l && x2 <= r))                printf("%.2lf\n",max(maxn,f2));            else if((x2 <= l || x2 >= r) && (x1 >= l && x1 <= r))                printf("%.2lf\n",max(maxn,f1));            else{                f1 = max(f1,f2);                printf("%.2lf\n",max(maxn,f1));            }        }    }    return 0;}




0 0
原创粉丝点击