1 #include <iostream> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstdio> 5 using namespace std; 6 int n; 7 typedef struct 8 { 9 double x; 10 double y; 11 }Point; 12 13 Point p[110],s[110]; 14 15 int top; 16 17 double Mul(Point a,Point b,Point c) 18 { 19 return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); 20 } 21 22 double dis(Point a,Point b) 23 { 24 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 25 } 26 27 int cmp(const void *a,const void *b) 28 { 29 Point c=*(Point *)a; 30 Point d=*(Point *)b; 31 double k=Mul(p[0],c,d); 32 if(k<0||(!k&&dis(c,p[0])>dis(d,p[0]))) return 1; 33 return -1; 34 } 35 36 void con() 37 { 38 int i; 39 for(i=1;i<n;i++) 40 { 41 Point temp; 42 if(p[i].y<p[0].y||(p[i].y==p[0].y&&p[i].x<p[0].x)) 43 { 44 temp=p[i]; 45 p[i]=p[0]; 46 p[0]=temp; 47 } 48 } 49 qsort(p+1,n-1,sizeof(p[0]),cmp); 50 s[0]=p[0]; 51 s[1]=p[1]; 52 s[2]=p[2]; 53 top=2; 54 for(i=3;i<n;i++) 55 { 56 while(top>=2&&Mul(s[top-1],s[top],p[i])<=0) top--; 57 top++; 58 s[top]=p[i]; 59 } 60 } 61 62 int main() 63 { 64 while(cin>>n,n) 65 { 66 int i; 67 for(i=0;i<n;i++) 68 { 69 cin>>p[i].x>>p[i].y; 70 } 71 if(n==1) 72 { 73 cout<<"0.00"<<endl; 74 continue; 75 } 76 else if(n==2) 77 { 78 printf("%.2f\n",dis(p[0],p[1])); 79 continue; 80 } 81 con(); 82 double coun=0; 83 for(i=0;i<top;i++) 84 { 85 coun+=dis(s[i],s[i+1]); 86 } 87 coun+=dis(s[top],s[0]); 88 printf("%.2f\n",coun); 89 } 90 return 0; 91 }
时间: 2024-10-14 22:54:13