HDU 5105 Math Problem(数学)

最大值求解
本文针对一个给定的三次多项式函数,在指定区间内求解其绝对值的最大值。通过讨论a是否为零来分情况求导数并找到临界点,最终确定最大值。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105


Problem Description
Here has an function:
   f(x)=|ax3+bx2+cx+d|(LxR)
Please figure out the maximum result of f(x).
 
Input
Multiple test cases(less than 100). For each test case, there will be only 1 line contains 6 numbers a, b, c, d, L and R. (10a,b,c,d10,100LR100)
 
Output
For each test case, print the answer that was rounded to 2 digits after decimal point in 1 line.
 
Sample Input
  
1.00 2.00 3.00 4.00 5.00 6.00
 
Sample Output
  
310.00
 
Source

PS:

分a等于零 和a不等于零的情况分别求导讨论!

代码如下:

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
    double a, b, c, d, L, R;
    while(~scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&L,&R))
    {
        double ansL = 0, ansR = 0;
        double maxans = 0;
        if(a != 0)
        {
            double t = 4*b*b-12*a*c;
            ansL = a*L*L*L+b*L*L+c*L+d;
            if(ansL < 0)
                ansL = -ansL;
            ansR = a*R*R*R+b*R*R+c*R+d;
            if(ansR < 0)
                ansR = -ansR;
            maxans = max(ansL,ansR);
            if(t >= 0)
            {
                double x1 = (-2*b+sqrt(t))/(6*a);
                double t1 = a*x1*x1*x1+b*x1*x1+c*x1+d;
                if(t1 < 0)
                    t1 = -t1;
                maxans = max(maxans,t1);
                double x2 = (-2*b-sqrt(t))/(6*a);
                double t2 = a*x2*x2*x2+b*x2*x2+c*x2+d;
                if(t2 < 0)
                    t2 = -t2;
                maxans = max(maxans,t2);
            }
        }
        else if(a == 0)
        {
            if(b != 0)
            {
                double tt = -c/(2*b);
                double t1 = a*tt*tt*tt+b*tt*tt+c*tt+d;
                if(t1 < 0)
                    t1 = -t1;
                maxans = t1;
            }
            double t1 = a*L*L*L+b*L*L+c*L+d;
            if(t1 < 0)
                t1 = -t1;
            maxans = max(maxans,t1);
            double t2 = a*R*R*R+b*R*R+c*R+d;
            if(t2 < 0)
                t2 = -t2;
            maxans = max(maxans,t2);
        }
        printf("%.2lf\n",maxans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值