hdu 5105(数学题)

Math Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2523    Accepted Submission(s): 606


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
 
题意:求f(x) = |a*x^3+b*x^2+c*x+d|方程的解
分情况讨论:
一:a==0
1.b=0
  那么极值就在f(l) 和 f(r)中.
2.b!=0 那么极值在 f(-b/(2*c)),f(l),f(r) 中
二:a!=0
  求导数得出去掉绝对值后原函数的导数为 3*a*x^2 + 2*b*x +c
delta = 4*b*b-12*a*c
1.delta>0
极值在 f(x1) f(x2) f(l) f(r) 中选择
2.delta<=0 极值在 f(l) 和 f(r) 中选择。
吃一堑长一智: a/2*b  !=  a/(2*b) WA了好多次啊~~~~~
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps = 1e-8;
int main(){
    double a,b,c,d,l,r;
    while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&l,&r)!=EOF){
        double ans = 0;
        if(fabs(a)<eps){
            ans = max(fabs(a*l*l*l+b*l*l+c*l+d),fabs(a*r*r*r+b*r*r+c*r+d));
            if(fabs(b)>eps){
                double mid = -c/(2*b);
                if(mid>=l&&mid<=r)
                ans = max(fabs(b*mid*mid+c*mid+d),ans);
            }
        }else{
            ans = max(fabs(a*l*l*l+b*l*l+c*l+d),fabs(a*r*r*r+b*r*r+c*r+d));
            if(4*b*b-12*a*c>0){
                double delta = 4*b*b-12*a*c;
                double x1 = (-2*b-sqrt(delta))/(6*a);
                double x2 = (-2*b+sqrt(delta))/(6*a);
                if(x1>=l&&x1<=r)
                ans = max(ans,fabs(a*x1*x1*x1+b*x1*x1+c*x1+d));
                if(x2>=l&&x2<=r)
                ans = max(ans,fabs(a*x2*x2*x2+b*x2*x2+c*x2+d));
            }
        }
        printf("%.2lf
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/liyinggang/p/5660287.html