ural 1348 Goat in the Garden 2

http://acm.timus.ru/problem.aspx?space=1&num=1348

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <cmath>

 4 #include <algorithm>

 5 using namespace std;

 6 const double eps=1e-10;

 7 

 8 double dcmp(double x){

 9     if(fabs(x)<eps) return 0;

10     else return x<0?-1:1;

11 }

12 

13 double sqr(double x)

14 {

15     return x*x;

16 }

17 struct node

18 {

19     double x,y;

20 }st3,st1,st2;

21 

22 double cross(node a,node b)

23 {

24     return (a.x*b.y-a.y*b.x);

25 }

26 double dot(node a,node b)

27 {

28     return (a.x*b.x+a.y*b.y);

29 }

30 double dis(double x1,double y1,double x2,double y2)

31 {

32     return sqrt(sqr(x1-x2)+sqr(y1-y2));

33 }

34 

35 double distance1(node p,node a,node b)

36 {

37     if(a.x==b.x&&a.y==b.y) return dis(p.x,p.y,a.x,a.y);

38     node p1; p1.x=b.x-a.x;p1.y=b.y-a.y;

39     node p2; p2.x=p.x-a.x;p2.y=p.y-a.y;

40     node p3; p3.x=p.x-b.x;p3.y=p.y-b.y;

41     if(dcmp(dot(p1,p2))<0) return dis(p.x,p.y,a.x,a.y);

42     else if(dcmp(dot(p1,p3))>0) return dis(p.x,p.y,b.x,b.y);

43     else return (fabs(cross(p1,p2))/dis(a.x,a.y,b.x,b.y));

44 }

45 

46 int main()

47 {

48     double x1,y1,x2,y2,x3,y3,l;

49     scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);

50     scanf("%lf%lf%lf",&x3,&y3,&l);

51     st1.x=x1;st1.y=y1;

52     st2.x=x2;st2.y=y2;

53     st3.x=x3;st3.y=y3;

54     double l1=dis(st3.x,st3.y,st1.x,st1.y);

55     double l2=dis(st3.x,st3.y,st2.x,st2.y);

56     double l3=distance1(st3,st1,st2);

57     double min1=min(l1,min(l2,l3));

58     double max1=max(l1,max(l2,l3));

59     double ans1=min1-l;

60     double ans2=max1-l;

61     if(ans1<eps) ans1=0;

62     if(ans2<eps) ans2=0;

63     printf("%.2lf\n",ans1);

64     printf("%.2lf\n",ans2);

65     return 0;

66 }
View Code

 

你可能感兴趣的:(Go)