您的位置:首页 > 其它

极角排序合集

2016-07-24 09:03 197 查看
poj 2007

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;

struct point{
int x,y;
bool operator<(point k)const{
int t=k.y*x-k.x*y;
return t>0?true:false;
}
}p[100];

int main()
{
int n=0;
while(scanf("%d%d",&p
.x,&p
.y)!=EOF)n++;
sort(p+1,p+n);
for(int i=0;i<n;i++){
printf("(%d,%d)\n",p[i].x,p[i].y);
}
return 0;
}

lightoj 1285 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;

const int maxn=2000+5;
struct point{
int x,y,id;
bool operator<(point b)const{
return x==b.x?y<b.y:x<b.x;
}
}p[maxn];

int len(int x,int y){
return x*x+y*y;
}

bool cmp(point z,point k){
int t=(k.y-p[0].y)*(z.x-p[0].x)-(k.x-p[0].x)*(z.y-p[0].y);
if(t==0)return len(z.x-p[0].x,z.y-p[0].y)<len(k.x-p[0].x,k.y-p[0].y);
return t>0;
}

int main()
{
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
p[i].id=i;
}
sort(p,p+n);
sort(p+1,p+n,cmp);
printf("Case %d:\n",cas);
point tt=p[n-1];
int pos=n-1;
for(int i=n-2;i>=0;i--){
int tmp=(tt.y-p[0].y)*(p[i].x-p[0].x)-(tt.x-p[0].x)*(p[i].y-p[0].y);
if(tmp!=0){
break;
}
pos=i;
}
if(pos==0){
printf("Impossible\n");continue;
}
reverse(p+pos,p+n);
for(int i=0;i<n;i++){
printf("%d",p[i].id);
if(i < n-1)printf(" ");
else printf("\n");
}
//printf("\n");
}
return 0;
}


poj 1696 Space Ant

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;

const int maxn=60+5;
int res[maxn];int pos;

struct point{
int x,y,id;
bool operator<(point b)const{
return y==b.y?x<b.x:y<b.y;
}
}p[maxn];

int len(int x,int y){
return x*x+y*y;
}

bool cmp(point z,point k){
int t=(k.y-p[pos].y)*(z.x-p[pos].x)-(k.x-p[pos].x)*(z.y-p[pos].y);
if(t==0)return len(z.x-p[pos].x,z.y-p[pos].y)<len(k.x-p[pos].x,k.y-p[pos].y);
return t>0;
}

int main()
{
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d%d%d",&p[i].id,&p[i].x,&p[i].y);
}
sort(p,p+n);
pos=0;int k=0;
for(int i=1;i<n;i++){
sort(p+i,p+n,cmp);
res[pos++]=p[k++].id;
}
res[pos++]=p[k++].id;
printf("%d",k);
for(int i=0;i<pos;i++){
printf(" %d",res[i]);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  极角排序