Three points for extremum time limitation: 10000ms single point time limit: 1000ms memory limit: 256MB description
This time we will be a little simpler, the topic here:
In a Cartesian coordinate system, there is a parabolic y=ax^2+bx+c and a point P (x, y), which is the shortest distance d from the parabolic line to the point P.
Input
Line 1th: 5 integer a,b,c,x,y. The first three numbers constitute the parabolic parameters, and the last two numbers x, y represent P-point coordinates. -200≤a,b,c,x,y≤200
Output
Line 1th: A real number D, reserved 3 decimal places (rounded)
Sample input
2 8 2-2 6
Sample output
2.437
#include <iostream> #include <math.h> #include <float.h> #include <iomanip>using namespace std; Double presult = 0.0;typedef struct funcparamter{double A; Double b; Double C;} funcparamter;typedef struct point{double x; Double y;} Point;double Calculator (funcparamter minefunc,point knownpoint,point foundpoint) {return sqrt ( Foundpoint.x-knownpoint.x) * (foundpoint.x-knownpoint.x) + (Minefunc.a*pow (foundpoint.x,2) +minefunc.b*foundpoint.x +MINEFUNC.C-KNOWNPOINT.Y) * (Minefunc.a*pow (foundpoint.x,2) +minefunc.b*foundpoint.x+minefunc.c-knownpoint.y));} Double Findy (double x,funcparamter minefunc) {return Minefunc.a*pow (x,2) +minefunc.b*x+minefunc.c;} Double Findmin (double left,double right,funcparamter minefunc,point knownpoint) {point lm,rm; lm.x = left+ (right-left)/3; rm.x = left+2* (right-left)/3; LM.Y = Findy (Lm.x,minefunc); RM.Y = Findy (Rm.x,minefunc); if ((Calculator (MINEFUNC,KNOWNPOINT,LM)-calculator (MINEFUNC,KNOWNPOINT,RM)) ==0) { Double min = Calculator (minefunc,knownpoint,lm) <calculator (MINEFUNC,KNOWNPOINT,RM)? Calculator (MINEFUNC,KNOWNPOINT,LM): Calculator (MINEFUNC,KNOWNPOINT,RM); Cout<<setiosflags (ios::fixed) <<setprecision (3) <<min<<endl; return min; } else if (Calculator (MINEFUNC,KNOWNPOINT,LM) <calculator (MINEFUNC,KNOWNPOINT,RM)) {Findmin (left,rm.x,minef Unc,knownpoint); } else {findmin (lm.x,right,minefunc,knownpoint); }}int Main () {Funcparamter minefunc; Point Knownpoint; cout<<dbl_max<<endl; cin>>minefunc.a>>minefunc.b>>minefunc.c>>knownpoint.x>>knownpoint.y; Cout<<calculator (Minefunc,knownpoint,foundpoint) <<endl; Findmin ( -1000,1000,minefunc,knownpoint); return 0;}
Tips
Three points and three points to find the Extremum