题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105
1.00 2.00 3.00 4.00 5.00 6.00
310.00
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;
}
原文地址:http://blog.csdn.net/u012860063/article/details/43114419