凸包算法 http://www.cnblogs.com/devymex/archive/2010/08/09/1795392.html #include <stdio.h> #include <algorithm> #include <math.h> using namespace std; #define N 101 #define INF 0x7ffffff struct T { double x,y; }t[N],stack[N]; double Mul(T p1,T p2,T p3)//叉积 { return (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y);//3点逆时针旋转时为正 } double dis(T a,T b) { double x=b.x-a.x; double y=b.y-a.y; return sqrt( x*x + y*y ); } int cmp(const T &a,const T &b) { double k=Mul(t[0],a,b); if (k<0 || (!k && dis(a,t[0]) > dis(b,t[0]) )) return 0;//根据head 逆时针旋转排列 return 1; } void Convex(int n,int &top) { int i; top=2; stack[0]=t[0]; stack[1]=t[1]; stack[2]=t[2]; for (i=3;i<=n;i++)//head 入栈2次 { while(Mul(stack[top-1],stack[top],t[i])<=0 && top>=2)//"<"和"<="是一样的 在一直线上的点可忽略 top--; top++; stack[top]=t[i]; } } int main () { freopen("1392.txt","r",stdin); int n,i,top,head; double x,y; T temp; while(scanf("%d",&n),n) { head=0; x=y=INF; for (i=0;i<n;i++) { scanf("%lf%lf",&t[i].x,&t[i].y); if (t[i].x<x || ( t[i].x==x && t[i].y<y ))//找head { x=t[i].x; y=t[i].y; head=i; } } if (n==1) puts("0.00"); else if (n==2) printf("%.2lf/n",dis(t[0],t[1])); else { temp=t[0]; t[0]=t[head]; t[head]=temp; sort(t+1,t+n,cmp); t[n]=t[0];//2个head Convex(n,top); double sum=0; for (i=0;i<top;i++) { sum+=dis(stack[i],stack[i+1]); printf("%.2lf %.2lf/n",stack[i].x,stack[i].y); } printf("%.2lf/n",sum); } } }