题目大意:乱序给出凸多边形的顶点坐标,要求按逆时针顺序输出各顶点。给的第一个点一定是(0,0),没有其他点在坐标轴上,没有三点共线的情况。
分析:利用叉积排序。
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
struct P {
int x, y;
P(){}
P(int x, int y):x(x), y(y) {}
};
P operator -(P p, P q) {
return P(p.x-q.x, p.y-q.y);
}
int operator ^(P p, P q) {
return p.x*q.y-p.y*q.x;
}
bool operator <(P p, P q) {<span style="white-space:pre"> //如果p1^p2 〉0,说明p1经逆时针旋转<180度可以到p2,则 p1 </span><<span style="white-space:pre"> p2</span>
if(((p-P(0, 0))^(q-P(0, 0))) < 0)
return false;
else return true;
}
P ps[60];
int main() {
int n = 0;
while(~scanf("%d%d", &ps[n].x, &ps[n].y)) {
n++;
}
sort(ps+1, ps+n);
for(int i = 0; i < n; i++)
printf("(%d,%d)\n", ps[i].x, ps[i].y);
return 0;
}