您的位置:首页 > 编程语言 > Go语言

poj_2007_Scrambled Polygon

2013-11-04 20:43 148 查看
题型:计算几何

题意:给出符合凸包的若干的点,第一个输入的点保证是原点,将这些点从原点开始按逆时针顺序输出。

分析:

题目中说没有在x轴上和y轴上的点(除了原点),所以极角排序排序的时候,将原点除外之后再排序,否则将得到错误答案(被坑死了,一排WA。。。)

由于木有说一共输入多少个点,所以用!=EOF处理,用文件输入输出来检测数据。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define eps 1e-10
using namespace std;

struct point {
double x,y;
} p[100];

//极角排序
double cross(const point &p1, const point &p2, const point &q1, const point &q2) {
return (q2.y - q1.y)*(p2.x - p1.x) - (q2.x - q1.x)*(p2.y - p1.y);
}
bool cmp(const point &a, const point &b) {
point origin;
origin.x = origin.y = 0;
return cross(origin,b,origin,a) < 0;
}

int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n=0;
while(~scanf("%lf%lf",&p
.x,&p
.y)) {
n++;
}
sort(p+1,p+n,cmp);
for (int i=0; i<n; i++) {
printf("(%.0lf,%.0lf)\n",p[i].x,p[i].y);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: