HDU5105 Math Problem(数学题)

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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值