angles角度

 

uva,10286

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <iostream>
 5 
 6 using namespace std;
 7 
 8 const double PI=3.141592653;
 9 
10 int main()
11 {
12     double F;
13     while(~scanf("%lf",&F))
14     {
15         printf("%.10lf\n",F*(sin(108*PI/180)/sin(63*PI/180)));
16     }
17     return 0;
18 }
View Code

用更佳精确的pi重写了。🐎的,我怎么就忘记了acos(-1)是pi了。。。我见了你的鬼了。看来数学和程序真的是分不开的啊。 

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <iostream>
 5 
 6 using namespace std;
 7 
 8 const double PI=acos(-1);
 9 
10 int main()
11 {
12     double F;
13     while(~scanf("%lf",&F))
14     {
15         printf("%.10lf\n",F*(sin(108*PI/180)/sin(63*PI/180)));
16     }
17     return 0;
18 }
View Code

 

uva,10180

为什么不用叉积然后判断距离了。。因为当叉积为0的时候不知道是0还是180所以要做特判很麻烦。因为sin在[0,180]内的两端点解不确定。如果用余弦定理cos就可以确定了。。wa了几次。注意了😄😄😄😄

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cmath>
  5 
  6 using namespace std;
  7 #define PI acos(-1)
  8 #define eps 1e-10
  9 
 10 
 11 double add(double a,double b)
 12 {
 13     if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
 14     return a+b;
 15 }
 16 
 17 struct node
 18 {
 19     double x,y;
 20     
 21     node(){}
 22     node(double x,double y):x(x),y(y){}
 23     
 24     double getdist(node p)
 25     {
 26         return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
 27     }
 28     
 29     double det(node p)
 30     {
 31         return add(x*p.y,-y*p.x);
 32     }
 33 };
 34 
 35 double dot(node p1,node p2,node p3)
 36 {
 37     return (p2.x-p1.x)*(p3.x-p1.x)+(p2.y-p1.y)*(p3.y-p1.y);
 38 }
 39 
 40 double angle(node p1,node p2,double r)
 41 {
 42     double d1=p1.getdist(node(0,0)),d2=p2.getdist(node(0,0)),d3=p1.getdist(p2);
 43     double C=acos((d1*d1+d2*d2-d3*d3)/(2.0*d1*d2));
 44     double c1=acos(r/d1);
 45     double c2=acos(r/d2);
 46     
 47     return sqrt(d1*d1-r*r)+sqrt(d2*d2-r*r)+(C-c1-c2)*r;
 48     
 49 }
 50 
 51 double getlen(node p1,node p2,double r)
 52 {
 53     if(p1.x==p2.x && p1.y==p2.y) return 0.0;
 54     
 55     if(p1.x==p2.x)
 56     {
 57         if(fabs(p1.x)>r)
 58         {
 59             return fabs(p1.y-p2.y);
 60         }
 61         else
 62         {
 63             if(p1.y*p2.y>0)
 64                 return fabs(p1.y-p2.y);
 65             else
 66                 return angle(p1,p2,r);
 67         }
 68     }
 69     
 70     if(p1.y==p2.y)
 71     {
 72         if(fabs(p1.y)>r)
 73             return fabs(p1.x-p2.x);
 74         else
 75         {
 76             if(p1.x*p2.x>0)
 77                 return fabs(p1.x-p2.x);
 78             else
 79                 return angle(p1,p2,r);
 80         }
 81     }
 82     
 83     if(fabs(p1.det(p2)/p1.getdist(p2))>=r)
 84         return p1.getdist(p2);
 85     else if(dot(p1,node(0,0),p2)<0 || dot(p2,node(0,0),p1)<0)
 86         return p1.getdist(p2);
 87     else
 88         return angle(p1,p2,r);
 89 }
 90 
 91 int main()
 92 {
 93     node p1,p2;
 94     double r;
 95     
 96     int n;
 97     scanf("%d",&n);
 98     while(n--)
 99     {
100         scanf("%lf%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y,&r);
101         printf("%.3lf\n",getlen(p1,p2,r));
102     }
103     return 0;
104 }
自己思路写出的题目简单的一览无余
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cmath>
  5 
  6 using namespace std;
  7 #define PI acos(-1)
  8 #define eps 1e-10
  9 
 10 
 11 double add(double a,double b)
 12 {
 13     if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
 14     return a+b;
 15 }
 16 
 17 struct node
 18 {
 19     double x,y;
 20     
 21     node(){}
 22     node(double x,double y):x(x),y(y){}
 23     
 24     double getdist(node p)
 25     {
 26         return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
 27     }
 28 };
 29 
 30 double dot(node p1,node p2,node p3)
 31 {
 32     return (p2.x-p1.x)*(p3.x-p1.x)+(p2.y-p1.y)*(p3.y-p1.y);
 33 }
 34 
 35 double angle(node p1,node p2,double r)
 36 {
 37     double d1=p1.getdist(node(0,0)),d2=p2.getdist(node(0,0)),d3=p1.getdist(p2);
 38     double C=acos((d1*d1+d2*d2-d3*d3)/(2.0*d1*d2));
 39     double c1=acos(r/d1);
 40     double c2=acos(r/d2);
 41     
 42     return sqrt(d1*d1-r*r)+sqrt(d2*d2-r*r)+(C-c1-c2)*r;
 43     
 44 }
 45 
 46 double getlen(node p1,node p2,double r)
 47 {
 48     if(p1.x==p2.x && p1.y==p2.y) return 0.0;
 49     
 50     if(p1.x==p2.x)
 51     {
 52         if(fabs(p1.x)>r)
 53         {
 54             return fabs(p1.y-p2.y);
 55         }
 56         else
 57         {
 58             if(p1.y*p2.y>0)
 59                 return fabs(p1.y-p2.y);
 60             else
 61                 return angle(p1,p2,r);
 62         }
 63     }
 64     
 65     if(p1.y==p2.y)
 66     {
 67         if(fabs(p1.y)>r)
 68             return fabs(p1.x-p2.x);
 69         else
 70         {
 71             if(p1.x*p2.x>0)
 72                 return fabs(p1.x-p2.x);
 73             else
 74                 return angle(p1,p2,r);
 75         }
 76     }
 77     double d1=p2.y-p1.y,d2=p2.x-p1.x;
 78     double d3=(double)p1.y*(p2.x-p1.x)-(double)p1.x*(p2.y-p1.y);
 79     
 80     if(fabs(d3)/sqrt((double)d1*d1+(double)d2*d2)>=r)
 81         return p1.getdist(p2);
 82     else if(dot(p1,node(0,0),p2)<0 || dot(p2,node(0,0),p1)<0)
 83         return p1.getdist(p2);
 84     else
 85         return angle(p1,p2,r);
 86 }
 87 
 88 int main()
 89 {
 90     node p1,p2;
 91     double r;
 92     
 93     int n;
 94     scanf("%d",&n);
 95     while(n--)
 96     {
 97         scanf("%lf%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y,&r);
 98         printf("%.3lf\n",getlen(p1,p2,r));
 99     }
100     return 0;
101 }
利用点到直线的距离判断。

 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <algorithm>
  5 
  6 using namespace std;
  7 #define eps 1e-10
  8 #define PI 3.141592653
  9 
 10 double add(double a,double b)
 11 {
 12     if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
 13     return a+b;
 14 }
 15 
 16 struct node
 17 {
 18     double x,y;
 19     node(){}
 20     
 21     node(double x,double y):x(x),y(y){}
 22     
 23     
 24     node operator-(node p){
 25         return node(add(x,-p.x),add(y,-p.y));
 26     }
 27     
 28     node operator+(node p){
 29         return node(add(x,p.x),add(y,p.y));
 30     }
 31     
 32     node operator*(double d){
 33         return node(x*d,y*d);
 34     }
 35     
 36     double det(node p){
 37         return add(x*p.y,-y*p.x);
 38     }
 39     double dot(node p){
 40         return add(x*p.x,y*p.y);
 41     }
 42 };
 43 
 44 double dist(node a,node b)
 45 {
 46     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
 47 }
 48 
 49 double r;
 50 
 51 
 52 double Angle(node a,node b)
 53 {
 54     double m=sqrt(a.x*a.x+a.y*a.y);
 55     double n=sqrt(b.x*b.x+b.y*b.y);
 56     double c=sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
 57     double C=acos((m*m+n*n-c*c)/(2.0*m*n));
 58     
 59     double anglem=acos(r/m);
 60     
 61     double anglen=acos(r/n);
 62     
 63     double len=r*(C-anglem-anglen);
 64     
 65     len+=(sqrt(m*m-r*r)+sqrt(n*n-r*r));
 66     return len;
 67 }
 68 
 69 double ropeLen(node p1,node p2)
 70 {
 71     if(p1.x==p2.x && p1.y==p2.y)
 72         return 0.0;
 73     
 74     if(p1.x==p2.x){
 75         if(fabs(p1.x)>r)
 76             return fabs(p1.y-p2.y);
 77         else
 78         {
 79             if(p1.y*p2.y>0)
 80                 return fabs(p1.y-p2.y);
 81             else
 82                 return Angle(p1,p2);
 83         }
 84     }
 85     
 86     if(p1.y==p2.y)
 87     {
 88         if(fabs(p1.y)>r)
 89             return fabs(p1.x-p2.x);
 90         else
 91         {
 92             if(p1.x*p2.x>0)
 93                 return fabs(p1.x-p2.x);
 94             else
 95                 return Angle(p1,p2);
 96         }
 97     }
 98     double slope=(double)(p2.y-p1.y)/(double)(p2.x-p1.x);
 99     double dis=fabs((double)(p2.y-slope*p2.x))/(double)(sqrt(1.0+slope*slope));
100     
101     if(dis>=r)
102         return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
103     else
104     {
105         double yIntersection=((double)(p2.y-slope*p2.x))/(slope*slope+1.0);
106         
107         if((p1.y-yIntersection)*(p2.y-yIntersection)>=0.0)
108             return sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
109         else
110             return Angle(p1,p2);
111         
112     }
113 }
114 
115 int main()
116 {
117     int n;
118     node p1,p2;
119     scanf("%d",&n);
120     while(n--)
121     {
122         scanf("%lf%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y,&r);
123         
124         printf("%.3lf\n",ropeLen(p1,p2));
125     }
126     return 0;
127 }
View Code

 

uva,10553

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <cstring>
 6 #include <string>
 7 #include <cstdlib>
 8 #include <map>
 9 
10 #define maxn 1005
11 #define eps 1e-10
12 using namespace std;
13 #define PI acos(-1)
14 //初始化
15 string ch[]={"N","NbE","NNE","NEbN","NE","NEbE","ENE","EbN","E","EbS","ESE",
16 "SEbE","SE","SEbS","SSE","SbE","S","SbW","SSW","SWbS","SW","SWbW","WSW","WbS","W","WbN",
17  "WNW","NWbW","NW","NWbN","NNW","NbW"};
18 
19 map<string,double> mp;
20 
21 //角度初始化
22 void ini()
23 {
24     for(int i=0;i<32;i++)
25         mp[ch[i]]=PI*(double(i))/(double)16;
26 }
27 
28 
29 //初始化节点。
30 struct node
31 {
32     double x,y;
33     
34     node(){}
35     node(double x,double y):x(x),y(y){}
36     
37     double getdis(node a){
38         return sqrt((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y));
39     }
40 };
41 
42 //判断点积,如果不在两线段间就至少有一个小与0
43 double getlen(node a,node b,node c)
44 {
45     return (b.x-a.x)*(c.x-a.x)+(b.y-a.y)*(c.y-a.y);
46 }
47 //求与线段之间的距离。
48 double dist(node a,node b,node c)
49 {
50     if(getlen(a,c,b)<0 || getlen(b,c,a)<0) return min(c.getdis(a),c.getdis(b));
51     else
52     {
53         double m=b.y-a.y,n=b.x-a.x;
54         double p=a.y*b.x-a.x*b.y;
55         return fabs(m*c.x-n*c.y+p)/sqrt(m*m+n*n);
56     }
57 }
58 
59 int main()
60 {
61     int n;
62     ini();
63     double dis[maxn],angle;
64     string s[maxn];
65     while(scanf("%d",&n),n)
66     {
67         node o(0,0),t(0,0),pre;
68         for(int i=0;i<n;i++)
69         {
70             cin>>s[i]>>dis[i];
71             //sin/cos都可以,只是走的方向不同。
72             o.x=o.x+sin(mp[s[i]])*dis[i];
73             o.y=o.y+cos(mp[s[i]])*dis[i];
74         }
75         cin>>angle;
76         double ans=o.getdis(t);
77         for(int i=0;i<n;i++)
78         {
79             pre=t;
80             t.x=t.x+sin(mp[s[i]]-angle*PI/double(180))*dis[i];
81             t.y=t.y+cos(mp[s[i]]-angle*PI/double(180))*dis[i];
82             
83             ans=min(ans,dist(pre,t,o));
84         }
85         
86         printf("%.2lf\n",ans);
87     }
88     return 0;
89 }
View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <cstring>
 6 #include <string>
 7 #include <map>
 8 #define PI acos(-1)//定义高精度的pi
 9 #define maxn 1005//定义转动方向的个数
10 
11 using namespace std;
12 
13 //初始化每个方向角。
14 string ch[]={"N","NbE","NNE","NEbN","NE","NEbE",
15     "ENE","EbN","E","EbS","ESE","SEbE","SE","SEbS",
16     "SSE","SbE","S","SbW","SSW","SWbS","SW","SWbW",
17     "WSW","WbS","W","WbN","WNW","NWbW","NW","NWbN","NNW","NbW"};
18 
19 
20 struct node
21 {
22     double x,y;
23     node(){}
24     node(double x,double y):x(x),y(y){}
25     
26     double getdist(node p)
27     {
28         return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
29     }
30 };
31 //把每个方向映射到具体弧度值
32 map<string,double> mp;
33 void init()
34 {
35     mp.clear();
36     for(int i=0;i<32;i++)
37     {
38         mp[ch[i]]=(double)(i)*PI/(double)16;
39     }
40 }
41 //求点积,判断一个点是否在一条线段之间
42 double dot(node p1,node p2,node p3)
43 {
44     return (p2.x-p1.x)*(p3.x-p1.x)+(p2.y-p1.y)*(p3.y-p1.y);
45 }
46 //求点到线段的最短距离。
47 double getdis(node p1,node p2,node p3)
48 {
49     if(dot(p1,p3,p2)<0 || dot(p2,p3,p1)<0) return min(p3.getdist(p1),p3.getdist(p2));
50     else
51     {
52         double d1=p2.y-p1.y,d2=p2.x-p1.x,d3=(double)p2.y*(p2.x-p1.x)-(double)p2.x*(p2.y-p1.y);
53         return fabs(d1*p3.x-d2*p3.y+d3)/sqrt(d1*d1+d2*d2);
54     }
55 }
56 
57 int main()
58 {
59     int n;
60     double dist[maxn],angle;
61     string s[maxn];
62     init();
63     while(scanf("%d",&n),n)
64     {
65         node o(0,0),t(0,0),pre;
66 //初始宝藏点
67         for(int i=0;i<n;i++)
68         {
69             cin>>s[i]>>dist[i];
70             o.x+=dist[i]*sin(mp[s[i]]);
71             o.y+=dist[i]*cos(mp[s[i]]);
72         }
73         cin>>angle;
74         double ans=o.getdist(node(0,0));
75 //正正北方走的宝藏点。
76         for(int i=0;i<n;i++)
77         {
78             pre=t;
79             t.x+=dist[i]*sin(mp[s[i]]-angle*PI/(double)180);
80             t.y+=dist[i]*cos(mp[s[i]]-angle*PI/(double)180);
81             ans=min(ans,getdis(t,pre,o));
82         }
83         printf("%.2lf\n",ans);
84     }
85     return 0;
86 }
自己的思想想出来的代码。

 

 

uva,132

多边形的内角和:n边形的内角和等于(n-2)x180

  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cmath>
  4 #include <cstdio>
  5 #include <vector>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <string>
  9 
 10 using namespace std;
 11 #define eps 1e-10
 12 #define inf 1e7
 13 
 14 double add(double a,double b)
 15 {
 16     if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
 17     return a+b;
 18 }
 19 
 20 struct node
 21 {
 22     double x,y;
 23     int num;
 24     node operator-(node p){
 25         return node(add(x,-p.x),add(y,-p.y));
 26     }
 27     
 28     double det(node p){
 29         return add(x*p.y,-y*p.x);
 30     }
 31     
 32     node(){}
 33     node(double x,double y):x(x),y(y){}
 34 };
 35 
 36 bool cmp(const node& a,const node& b)
 37 {
 38     if(a.y==b.y) return a.x<b.x;
 39     return a.y<b.y;
 40 }
 41 
 42 vector<node> convex_hull(vector<node> ps,int n)
 43 {
 44     sort(ps.begin(),ps.end(),cmp);
 45     int k=0;
 46     vector<node> qs(n*2);
 47     for(int i=0;i<n;i++){
 48         while(k>1 && (qs[k-1]-qs[k-2]).det(ps[i]-qs[k-2])<0) k--;
 49         qs[k++]=ps[i];
 50     }
 51     
 52     for(int i=n-2,t=k;i>=0;i--){
 53         while(k>t && (qs[k-1]-qs[k-2]).det(ps[i]-qs[k-2])<0) k--;
 54         qs[k++]=ps[i];
 55     }
 56     qs.resize(k);
 57     return qs;
 58 }
 59 
 60 double getlen(node a,node b,node c)
 61 {
 62     return (b.x-a.x)*(c.x-a.x)+(b.y-a.y)*(c.y-a.y);
 63 }
 64 
 65 bool turnRight(node a,node b,node c)
 66 {
 67     return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y)>=0;
 68 }
 69 
 70 int solve(node mass,vector<node> p,int n)
 71 {
 72     vector<int> ans(n*n,0);
 73     int ddv=1;
 74     int cnt=0;
 75     node l=p[0],r=p[1];
 76     if(!(getlen(l,mass,r)<0 || getlen(r,mass,l)<0) && turnRight(l,r,mass))
 77     {
 78         ans[cnt]=max(l.num,r.num);
 79         cnt++;
 80     }
 81     int k=2;
 82     while(k<n)
 83     {
 84         if((r-l).det(p[k]-l)>0)
 85         {
 86             l=p[k-1],r=p[k];
 87             ddv=k;
 88             if(!(getlen(l,mass,r)<0 || getlen(r,mass,l)<0) && turnRight(l,r,mass))
 89             {
 90                 ans[cnt]=max(ans[cnt],max(r.num,l.num));
 91                 cnt++;
 92             }
 93         }
 94         else if((r-l).det(p[k]-l)==0)
 95         {
 96             while((r-l).det(p[k]-l)==0 && k<n)
 97             {
 98                 
 99                 if(!(getlen(l,mass,p[k])<0 || getlen(p[k],mass,l)<0)&& turnRight(l,p[k],mass))
100                 {
101                     ans[cnt]=max(ans[cnt],max(l.num,p[k].num));
102                     cnt++;
103                     for(int m=ddv;m<k;m++)
104                     {
105                         if(!(getlen(p[m],mass,p[k])<0 || getlen(p[k],mass,p[m])<0)&& turnRight(p[m],p[k],mass))
106                         {
107                             ans[cnt]=max(ans[cnt],max(p[m].num,p[k].num));
108                             cnt++;
109                         }
110                     }
111                 }
112                 k++;
113             }
114             if(k!=n)
115                 k--;
116         }
117         k++;
118     }
119     
120     int minx=inf;
121     for(int i=0;i<cnt;i++)
122     {
123         minx=min(minx,ans[i]);
124     }
125     return minx;
126 }
127 
128 int main()
129 {
130     string s;
131     vector<node> p;
132     while(cin>>s && s!="#")
133     {
134         node mass;
135         p.clear();
136         int n=0;
137         int x,y;
138         scanf("%lf%lf",&mass.x,&mass.y);
139         while(scanf("%d%d",&x,&y) && x+y)
140         {
141             p.push_back(node(x,y));
142             p[n].num=n+1;
143             n++;
144         }
145         
146         p=convex_hull(p,(int)p.size());
147         cout<<s<<' '<<solve(mass,p,(int)p.size())<<endl;
148     }
149     return 0;
150 }
纪念一份想多了的代码
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <string>
  6 #include <algorithm>
  7 
  8 #include <climits>
  9 using namespace std;
 10 
 11 #define eps 1e-6
 12 
 13 struct node{
 14     double x,y;
 15     
 16     node(double a=0,double b=0):x(a),y(b){}
 17     
 18     bool operator<(const node& a)const{
 19         if(fabs(x-a.x)>eps)
 20             return x<a.x;
 21         return y<a.y;
 22     }
 23     
 24     node operator-(const node& a)const{
 25         node ret;
 26         ret.x=x-a.x;
 27         ret.y=y-a.y;
 28         return ret;
 29     }
 30 };
 31 
 32 
 33 double dot(node a,node b){
 34     return a.x*b.x+a.y*b.y;
 35 }
 36 
 37 
 38 double cross(node o,node a,node b){
 39     return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
 40 }
 41 
 42 int between(node a,node b,node c){
 43     return dot(c-a,b-a)>=0 && dot(c-b,a-b)>=0;
 44 }
 45 
 46 int onSeg(node a,node b,node c){
 47     return between(a,b,c) && fabs(cross(a,b,c))<eps;
 48 }
 49 
 50 int convex_hull(int n,node p[],node ch[])
 51 {
 52     sort(p,p+n);
 53     
 54     int i,m=0,t;
 55     for(i=0;i<n;i++){
 56         while(m>=2 && cross(ch[m-2],ch[m-1],p[i])<=0)
 57             m--;
 58         ch[m++]=p[i];
 59     }
 60     
 61     for(i=n-1,t=m+1;i>=0;i--){
 62         while(m>=t && cross(ch[m-2],ch[m-1],p[i])<=0)
 63             m--;
 64         ch[m++]=p[i];
 65     }
 66     
 67     return m-1;
 68 }
 69 
 70 int main()
 71 {
 72     string s;
 73     
 74     node p[1024],ch[1024],In[1024];
 75     while(cin>>s && s!="#"){
 76         double x,y;
 77         
 78         int n=0,m;
 79         
 80         node mass;
 81         
 82         scanf("%lf%lf",&mass.x,&mass.y);
 83         while(scanf("%lf%lf",&x,&y) && x+y)
 84         {
 85             In[n]=p[n]=node(x,y);
 86             n++;
 87         }
 88         
 89         m=convex_hull(n,p,ch);
 90         
 91         int ret=0x3f3f3f3f;
 92         for(int i=0,j=m-1;i<m;j=i++)
 93         {
 94             if(between(ch[i],ch[j],mass)){
 95                 int mn=0;
 96                 
 97                 for(int k=0;k<n;k++)
 98                     if(onSeg(ch[i],ch[j],In[k]))
 99                         mn=max(mn,k);
100                 ret=min(ret,mn);
101             }
102         }
103         
104         cout<<s<<' '<<ret+1<<endl;
105     }
106     return 0;
107 }
View Code

 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <algorithm>
  5 #include <cstring>
  6 #include <string>
  7 #include <climits>
  8 
  9 using namespace std;
 10 #define maxn 1024
 11 #define eps 1e-10
 12 
 13 double add(double a,double b)
 14 {
 15     if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
 16     return a+b;
 17 }
 18 
 19 struct node
 20 {
 21     double x,y;
 22     node(){}
 23     node(double x,double y):x(x),y(y){}
 24     
 25     node operator-(node p)
 26     {
 27         return node(add(x,-p.x),add(y,-p.y));
 28     }
 29     
 30     node operator+(node p)
 31     {
 32         return node(add(x,p.x),add(y,p.y));
 33     }
 34     
 35     double dot(node p)
 36     {
 37         return add(x*p.x,y*p.y);
 38     }
 39     
 40     double det(node p)
 41     {
 42         return add(x*p.y,-y*p.x);
 43     }
 44 };
 45 
 46 bool cmp(const node& a,const node& b)
 47 {
 48     if(a.y==b.y) return a.x<b.x;
 49     return a.y<b.y;
 50 }
 51 
 52 bool OnSegment(node p1,node p2,node p3)
 53 {
 54     if(min(p1.x,p2.x)<=p3.x && p3.x<=max(p1.x,p2.x) && min(p1.y,p2.y)<=p3.y && p3.y<=max(p1.y,p2.y))
 55         return true;
 56     return false;
 57 }
 58 
 59 bool between(node p1,node p2,node p3)
 60 {
 61     return (p2-p1).dot(p3-p1)>=0 && (p2-p3).dot(p1-p3)>=0;
 62 }
 63 int convex_hull(node p[],int n,node CH[])
 64 {
 65     sort(p,p+n,cmp);
 66     int k=0;
 67     
 68     
 69     for(int i=0;i<n;i++)
 70     {
 71         while(k>1 && (CH[k-1]-CH[k-2]).det(p[i]-CH[k-2])<=0) k--;
 72         CH[k++]=p[i];
 73     }
 74     
 75     for(int i=n-2,t=k;i>=0;i--)
 76     {
 77         while(k>t && (CH[k-1]-CH[k-2]).det(p[i]-CH[k-2])<=0) k--;
 78         CH[k++]=p[i];
 79     }
 80     return k-1;
 81 }
 82 
 83 int main()
 84 {
 85     string s;
 86     node mass;
 87     node CH[maxn];
 88     node p[maxn],q[maxn];
 89     while(cin>>s && s!="#")
 90     {
 91         scanf("%lf%lf",&mass.x,&mass.y);
 92         double x,y;
 93         int n=0;
 94         while(scanf("%lf%lf",&x,&y) && x+y)
 95         {
 96             q[n]=p[n]=node(x,y);
 97             n++;
 98         }
 99         
100         int m=convex_hull(p,n,CH);
101         int ans=INT_MAX;
102 //判断凸包两顶点之间点的最大编号,然后去最小,最简单的遍历。学习了
103 //强大的路
104         for(int i=0,j=m-1;i<m;j=i++)
105         {
106             if(between(CH[i],mass,CH[j]))
107             {
108                 int cnt=0;
109                 for(int k=0;k<n;k++)
110                 {
111                     if(OnSegment(CH[i],CH[j],q[k]))
112                     {
113                         cnt=max(cnt,k);
114                     }
115                 }
116                 ans=min(ans,cnt);
117             }
118         }
119         cout<<s<<' '<<ans+1<<endl;
120     }
121     return 0;
122 }
View Code

 

posted @ 2016-05-21 18:35  指尖泛出的繁华  阅读(272)  评论(0编辑  收藏  举报