UPDATE2023-08-16:更新文章
Description
有一条抛物线y=ax^2+bx+c和一个点P(x,y),求点P到抛物线的最短距离d。
Input
每行有5个整数a,b,c,x,y。前三个数构成抛物线的参数,后两个数x,y表示P点坐标。-200≤a,b,c,x,y≤200
Output
每行1个实数d,保留3位小数(四舍五入)
Sample Input
2 8 2 -2 6
Sample Output
2.437
代码
#include<bits/stdc++.h>
using namespace std;
//v1.0 beta5
double a, b, c, x1, Y1, L, R, Lmid, Rmid;
int n,t;
double cal(double x2)
{
return sqrt((x2 - x1) * (x2 - x1) + (a * x2 * x2 + b * x2+ c - Y1) * (a * x2 * x2 + b * x2 + c - Y1));
}
int main()
{
cin >> a >> b >> c >> x1 >> Y1;
L = -200; R = 200;
double eps = 1e-5;
while (R - L >= eps)
{
Lmid = L + (R - L) / 3;
Rmid = R - (R - L) / 3;
if (cal(Lmid) <= cal(Rmid))
R = Rmid;
else
L = Lmid;
}
cout << fixed << setprecision(3) << cal(L) << endl;
return 0;
}