首页 > 代码库 > HDU 5105 Math Problem(数学)

HDU 5105 Math Problem(数学)

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


Problem Description
Here has an function:
  f(x)=|a?x技术分享3技术分享+b?x技术分享2技术分享+c?x+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
BestCoder Round #18 

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


HDU 5105 Math Problem(数学)