标签:poi distance namespace for pac log stream sqrt ret
给定一个点的坐标(x, y),在输入的n个点中,依次计算这些点到指定点的距离,并按照距离进行从小到大排序,并且输出点的坐标(如果距离相同,将x轴坐标比较小的点排到前面, 如果距离相等且x轴坐标也相同,则将y轴坐标较小的点排到前面)。坐标为int类型,范围为-1000到1000。n 为1到100之间正整数。
0 0
5
1 0 0 1 1 1 5 2 4 0
(0,1), (1,0), (1,1), (4,0), (5,2)
代码:
#include<iostream> #include<stdio.h> #include<math.h> using namespace std; struct point { int pos_x; int pos_y; int distance; }; int main() { int pos_x1,pos_y1; struct point pos[101]; int n,i,j; cin>>pos_x1>>pos_y1; cin>>n; for(i=0;i<n;i++) { cin>>pos[i].pos_x>>pos[i].pos_y; pos[i].distance=0; } for(j=0;j<n;j++) { pos[j].distance=sqrt((pos[j].pos_x-pos_x1)*(pos[j].pos_x-pos_x1)+(pos[j].pos_y-pos_y1)*(pos[j].pos_y-pos_y1)); } struct point temp; for(i=0;i<n-1;i++) { for(j=0;j<n-1-i;j++) { if(pos[j].distance>pos[j+1].distance) { temp=pos[j]; pos[j]=pos[j+1]; pos[j+1]=temp; } } } for(i=0;i<n-1;i++) { for(j=0;j<n-1-i;j++) { if((pos[j].distance==pos[j+1].distance)&&(pos[j].pos_x>pos[j+1].pos_x)) { temp=pos[j]; pos[j]=pos[j+1]; pos[j+1]=temp; } else if((pos[j].distance==pos[j+1].distance)&&(pos[j].pos_x==pos[j+1].pos_x)&&(pos[j].pos_y>pos[j+1].pos_y)) { temp=pos[j]; pos[j]=pos[j+1]; pos[j+1]=temp; } } } for(i=0;i<n;i++) { printf("(%d,%d) ",pos[i].pos_x,pos[i].pos_y); } cout<<endl; return 0; }
标签:poi distance namespace for pac log stream sqrt ret
原文地址:http://www.cnblogs.com/gcter/p/7429511.html