题目大意
给出一个参考点,再给出n个点,求这n个点中距参考点欧几里得最近的3 个点的编号。如果距离相同,则按照编号升序排序。
解题思路
难点是输出的是编号,且距离相同时,把编号小的排在前面。定义结构体,存储编号和距离。使用sort()函数进行排序,利用cmp()函数定义排序规则。
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn = 2005;
int x, y, n;
struct node{
int id;
double dis;
}pos[maxn];
double get(double x, double y, double xx, double yy){
return (x - xx)*(x - xx) + (y - yy)*(y - yy);
}
bool cmp(node a, node b){ return a.dis == b.dis ? a.id < b.id : a.dis < b.dis; }
int main(){
scanf("%d%d%d", &n, &x, &y);
for (int i = 1, tx, ty; i <= n; i++){
scanf("%d%d", tx, ty);
pos[i].id = i;
pos[i].dis = get(tx, ty, x, y);
}
sort(pos + 1, pos + n, cmp);
for (int i = 1; i <= 3; i++) printf("%d\n", pos[i].id);
return 0;
}