找了挺多题验的,一个小小的细节不注意就会WA掉。 boj 1673 boj 1251 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define MAXN 50005 #define pi acos(-1.0) #define eps 1e-8 #define inf 200000000 using namespace std; struct point{ double x,y; }; int n; point p[MAXN];//读入的顶点 int sta[MAXN];//保存凸包 double dis(point a,point b)//亮点间的距离 { return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y)); } double detmul(point a,point b,point c)//叉积 { return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); } bool cmp(point a,point b)//极角排序 { double ret=detmul(p[0],a,b); if (ret>eps||(fabs(ret)<eps&&dis(a,p[0])<dis(b,p[0]))) return true; return false; } int convex()//返回凸包的顶点,编号是p[0~top] { int i,top=1,id=0; for (i=1;i<n;i++)//找出最左最下的顶点 if ((p[i].y<p[id].y)||(fabs(p[i].y-p[id].y)<eps&&p[i].x+eps<p[id].x)) id=i; point tmp=p[0]; p[0]=p[id]; p[id]=tmp; sort(p+1,p+n,cmp); sta[0]=0; sta[1]=1; for (i=2;i<n;i++){ while (top>=1&&detmul(p[sta[top-1]],p[sta[top]],p[i])<=0) top--; sta[++top]=i; } return top+1; } double slove() { //特别注意一个顶点或两个顶点的特殊情况 }