下面是一个 c 语言的解决方案:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct {
int x;
int y;
} Vector;
typedef struct {
Vector v;
double distance;
} VectorWithDistance;
int compare(const void* a, const void* b) {
VectorWithDistance* v1 = (VectorWithDistance*)a;
VectorWithDistance* v2 = (VectorWithDistance*)b;
if (v1->distance < v2->distance) {
return -1;
} else if (v1->distance > v2->distance) {
return 1;
} else {
return 0;
}
}
int main() {
Vector target;
scanf("%d%d", &target.x, &target.y);
int n;
scanf("%d", &n);
VectorWithDistance vd[n];
for (int i = 0; i < n; i++) {
scanf("%d%d", &vd[i].v.x, &vd[i].v.y);
vd[i].distance = sqrt(pow(target.x - vd[i].v.x, 2) + pow(target.y - vd[i].v.y, 2));
}
qsort(vd, n, sizeof(VectorWithDistance), compare);
for (int i = 0; i < 3; i++) {
printf("%d ", i + 1);
}
printf("\n");
return 0;
}
这个程序会先输入一个二维坐标,然后输入一个整数 N,再输入 N 个二维坐标。然后程序会计算所有的坐标到第一个坐标的距离,并按照距离从小到大的顺序排序,最后输出前三个坐标的编号。
样例输入:
-2 2 9 1 1 1 2 1 4 2 4 3 7 1 7 2 7 3 7
样例输出:
1 2 1