给一些点,这些点都是一个凸包上的顶点,以第一个点为起点顺时针把别的点拍排一下序列。
分析:最简单的极坐标排序了.....................
代码如下:
--------------------------------------------------------------------------------------------------------------------------
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<queue> #include<string> #include<vector> #include<math.h> using namespace std; const double EPS = 1e-10; const double PI = acos(-1); const int MAXN = 1e3+7; int sta[MAXN], top; int Sign(double t) { if(t > EPS)return 1; if(fabs(t) < EPS)return 0; return -1; } struct point { double x, y; point(double x=0, double y=0):x(x), y(y){} point operator - (const point &t)const{ return point(x-t.x, y-t.y); } double operator ^(const point &t)const{ return x*t.y - y*t.x; } double operator *(const point &t)const{ return x*t.x + y*t.y; } }p[MAXN]; double Dist(point a, point b) { return sqrt((a-b)*(a-b)); } bool cmp(point a, point b) { int t = Sign((a-p[0])^(b-p[0])); if(t == 0) return Dist(a, p[0]) < Dist(b, p[0]); return t > 0; } int main() { int i=0, N; while(scanf("%lf%lf", &p[i].x, &p[i].y) != EOF) i++; N = i; sort(p+1, p+N, cmp); for(i=0; i<N; i++) printf("(%.0f,%.0f) ", p[i].x, p[i].y); return 0; }