裸题~~+模板!!!
View Code
1 /* 2 几何 凸包 3 顺时针!!! 4 */ 5 #include<stdio.h> 6 #include<string.h> 7 #include<stdlib.h> 8 #include<algorithm> 9 #include<iostream> 10 #include<queue> 11 //#include<map> 12 #include<math.h> 13 using namespace std; 14 typedef long long ll; 15 //typedef __int64 int64; 16 const int maxn = 105; 17 const int inf = 0x7fffffff; 18 const int pi=acos(-1.0); 19 struct node{ 20 int x,y; 21 bool operator <( const node &a ) const { 22 return y<a.y||(y==a.y&&x<a.x); 23 } 24 }; 25 26 node pnt[ maxn ],res[ maxn ]; 27 28 int cross( node sp,node ep,node op ){ 29 return (sp.x - op.x) * (ep.y - op.y)-(ep.x - op.x) * (sp.y - op.y); 30 } 31 /* 32 ep 33 | 34 | 35 op----sp 36 ( from sp to ep ) 37 */ 38 39 double dis( node a,node b ){ 40 double sum=0; 41 sum=1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y); 42 return sqrt( sum ); 43 }//两点之间的距离 44 45 int graham( int n ){ 46 int top=1; 47 sort( pnt,pnt+n ); 48 if( n==0 ) return 0; 49 else res[ 0 ]=pnt[ 0 ]; 50 if( n==1 ) return 1; 51 else res[ 1 ]=pnt[ 1 ]; 52 if( n==2 ) return 2; 53 else res[ 2 ]=pnt[ 2 ]; 54 55 for( int i=2;i<n;i++ ){ 56 while( top&&cross( res[ top-1 ],pnt[ i ],res[ top ] )>=0 ) 57 top--; 58 res[ ++top ]=pnt[ i ]; 59 } 60 61 int len=top; 62 res[ ++top ]=pnt[ n-2 ]; 63 for( int i=n-3;i>=0;i-- ){ 64 while( top!=len&&cross( res[ top-1 ],pnt[ i ],res[ top ] )>=0 ) 65 top--; 66 res[ ++top ]=pnt[ i ]; 67 } 68 69 return top;//返回res中的点的个数 70 } 71 72 int main(){ 73 int n; 74 while( scanf("%d",&n)==1,n ){ 75 for( int i=0;i<n;i++ ) 76 scanf("%d%d",&pnt[ i ].x,&pnt[ i ].y); 77 if( n==1 ) 78 { 79 printf("0.00\n"); 80 continue; 81 } 82 if( n==2 ) 83 { 84 printf("%.2lf\n",dis( pnt[0],pnt[1] ) ); 85 continue; 86 }//注意这两个情况的讨论!!! 87 int cnt=graham( n ); 88 double ans=0; 89 for( int i=0;i<cnt;i++ ){ 90 if( i==cnt-1 ){ 91 ans+=( dis( res[ cnt-1 ],res[ 0 ] ) ); 92 } 93 else{ 94 ans+=( dis( res[ i ],res[ i+1 ] ) ); 95 } 96 } 97 printf("%.2lf\n",ans); 98 } 99 return 0; 100 }