最大值无非就是在两个端点或极值点处取得。
我注意讨论了a=0和b=0,却忽略了极值点可能不在L到R的范围内这一问题。被Hack了。
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm> #include<stack> #include<queue> #include<cctype> #include<sstream> using namespace std; #define pii pair<int,int> #define LL long long int const int eps=1e-8; const int INF=1000000000; const int maxn=10000+10; double a,b,c,d,L,R,ans; double f(double x) { return fabs(a*x*x*x+b*x*x+c*x+d); } int main() { //freopen("in10.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&L,&R)==6) { if(a!=0) { if(a<0) { a=-a; b=-b; c=-c; d=-d; } double pan=b*b-3*a*c; if(pan<=0) { printf("%.2f ",max(f(L),f(R))); } else { double x1=(-sqrt(pan)-b)/(3*a); double x2=(sqrt(pan)-b)/(3*a); if(x1>=L&&x1<=R&&x2>=L&&x2<=R) printf("%.2f ",max(max(f(L),f(R)),max(f(x1),f(x2)))); else if(x1>=L&&x1<=R) printf("%.2f ",max(f(L),max(f(R),f(x1)))); else if(x2>=L&&x2<=R) printf("%.2f ",max(f(L),max(f(R),f(x2)))); else printf("%.2f ",max(f(L),f(R))); } } else { if(b==0) printf("%.2f ",max(f(L),f(R))); else { double z=(-c)/(2*b); if(z<=R&&z>=L) printf("%.2f ",max(f(L),max(f(R),f(z)))); else printf("%.2f ",max(f(L),f(R))); } } } //fclose(stdin); //fclose(stdout); return 0; }