写了好久,还是不甚明白
#include<math.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct point
{ double x; double y;
};
point s[102],dian[102];
int n;
double chacheng2(point p0,point p1,point p2)
{ return (p2.x-p0.x)*(p1.y-p0.y)-(p1.x-p0.x)*(p2.y-p0.y);
}
double sheng(point p1,point p2)
{ return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
bool cmp(point p1,point p2)
{ if(p1.y<p2.y) return true;
if(p1.y>p2.y) return false;
if(p1.x<p2.x) return true;
}
int tubao()
{
int i,k;
k=2; s[0]=dian[0]; s[1]=dian[1];
for(i=2;i<n;i++)
{
while(k>=2&&chacheng2(s[k-2],s[k-1],dian[i])>=0) --k;
s[k++]=dian[i];
}
int r = k;
for(i = n-2; i >= 0; i--)
{
while(k>r&&chacheng2(s[k-2],s[k-1],dian[i])>=0) --k;
if(i != 0)
s[k++] = dian[i];
}
return k;
}
double zong(int k)
{ int i;
double sum=0;
for(i=0;i<k-1;i++)
{ sum+=sheng(s[i], s[i+1]);
}
sum+=sheng(s[k-1], s[0]);
return sum;
}
int main()
{ int i,j,k;
while(scanf("%d",&n)!=EOF && n)
{
for(i=0;i<n;i++)
scanf("%lf%lf",&dian[i].x,&dian[i].y);
if(n==1){ printf("0.00\n");continue;}
if(n==2)
{
printf("%.2lf\n", sheng(dian[0],dian[1]));
continue;
}
//逆时针方向排序
sort(dian,dian+n,cmp);
k=tubao();
printf("%.2lf\n",zong(k));
}
return 0;
}