Topic Portal: Click here
This problem is actually a high school math problem. For absolute worth the maximum value is nothing more than a point or two vertex. So we just have to discuss it separately.
1, a = 0 o'clock, this time the function is f (x) = Bx2 + cx+d
①b = 0 o'clock, when the equation becomes f (x) = cx + D, when the function is linearly variable, the maximum value appears at both ends.
②b≠0, this time the equation is f (x) = bx2 + cx + D, when the function becomes two times.
At this point the value of the function is at axis z =-c/2b or both ends.
2, a≠0, the function is f (x) = ax3 + bx2 + cx + D, this situation is slightly troublesome, it is necessary to use the derivative to judge.
The derivative of the function is F ' (x) = 3ax2 + 2BX + c,δ= (2b) 2-4*3a*c = 4 (B2-3AC).
① when δ≤0, f (x) is a monotone function, so the maximum value appears at both ends.
② when δ> 0 o'clock, make f ' (x) = 0 => x1 = (-sqrt (δ)-2b)/(2*3a), x2 = (sqrt (δ)-2b)/(2*3A)
(Ⅰ) When x1,x2 are within the interval [l,r], the maximum value is in F (L), F (R), F (x1), F (x2)
(Ⅱ) when X1 or X2 is only one within [l,r], the maximum value is in F (L), F (R), F (x1) or F (x2)
(Ⅲ) When x1,x2 are not within the interval [l,r], the maximum value is in F (L), F (R)
The code is as follows:
#include <math.h> #include <stdio.h>double a,b,c,d,l,r;double disc,x1,x2,z;double max (double a,double b) { return a>b?a:b;} Double f (Double x) { if (x>=l && x<=r) return fabs (a*x*x*x + b*x*x + c*x + D); return-1;} int main () { while (scanf ("%lf%lf%lf%lf%lf%lf", &a,&b,&c,&d,&l,&r) ==6) { if (a!=0) { disc = B*b-3*a*c; if (disc<=0) { printf ("%.2f\n", Max (f (L), F (R))); } else{ x1= (-sqrt (disc)-B)/(3*a); X2= (sqrt (disc)-B)/(3*a); printf ("%.2f\n", Max (Max (f (L), F (R)), Max (f (x1), F (x2)))); } } else{ if (b==0) printf ("%.2f\n", Max (f (L), F (R))); else{ z = (-c)/(2*B); printf ("%.2f\n", Max (f (L), Max (f (R), F (z)))); }}} return 0;}
Because the HDU data is not perfect, so it can be (of course, there are bugs)
#include <math.h> #include <stdio.h>double a,b,c,d,l,r;double max (double a,double b) { return A>b?a: b;} Double f (Double x) { if (x>=l && x<=r) return fabs (a*x*x*x + b*x*x + c*x + D); return-1;} int main () { double disc,x1,x2,z; while (scanf ("%lf%lf%lf%lf%lf%lf", &a,&b,&c,&d,&l,&r) ==6) { disc = B*b-3*a*c,z = (-c)/( b); X1 = (-SQRT (disc)-B)/(3*a), x2 = (sqrt (disc)-B)/(3*a); printf ("%.2f\n", Max (Max (f (L), Max (f (R), F (z))), Max (f (x1), F (x2)))); } return 0;}
hdu5105 Math problem