C++-hihoCoder-1142-三分求极值[三分]
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef double db; 4 const db eps=1e-9; 5 db a,b,c,x,y; 6 db dis2(db X){ 7 db Y=a*X*X+b*X+c; 8 return (X-x)*(X-x)+(Y-y)*(Y-y); 9 } 10 db third(){ 11 db l=-200,r=200,ans1,ans2; 12 while(r-l>eps){ 13 db ll=(2*l+r)/3,rr=(l+2*r)/3; 14 ans1=dis2(ll);ans2=dis2(rr); 15 ans1<ans2?r=rr:l=ll; 16 } 17 return sqrt(ans1); 18 } 19 int main(){ 20 scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&x,&y); 21 printf("%.3f\n",third()); 22 return 0; 23 }
~~Jason_liu O(∩_∩)O