Description
题解
代码
1 #include <cmath> 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 #define ll long long 6 using namespace std; 7 double a; 8 double dis(ll x,ll y) { return sqrt(x*x+y*y); } 9 ll cross(ll x1,ll y1,ll x2,ll y2) { return x1*y2-x2*y1; } 10 double getcos(ll x1,ll y1,ll x2,ll y2) { return (double)(x1*x2+y1*y2)/(dis(x1,y1)*dis(x2,y2)); } 11 int main() 12 { 13 freopen("math.in","r",stdin),freopen("math.out","w",stdout); 14 ll x1,y1,x2,y2,k; 15 while (scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2)!=EOF) 16 { 17 if (cross(x1,y1,x2,y2)==0) { printf("0\n"); continue; } 18 while (1) 19 { 20 if (dis(x1,y1)>dis(x2,y2)) swap(x1,x2),swap(y1,y2); 21 a=getcos(x1,y1,x2,y2); 22 if (a<0) { x1=-x1,y1=-y1;continue; } 23 if (a<0.5) break; 24 k=dis(x2,y2)*a/dis(x1,y1)+0.5,x2-=k*x1,y2-=k*y1; 25 } 26 printf("%lld\n",min(x1*x1+y1*y1,x2*x2+y2*y2)); 27 } 28 }