使用 结构体
struct
+ 自定义sort
轻松搞定
#include<iostream> #include<bits/stdc++.h> #include<cstdio> using namespace std; int n, X, Y; struct point { int i; int dist; }po[205]; bool cmp(point a, point b){ if (a.dist==b.dist) return a.i < b.i; return a.dist < b.dist; } int main() { cin >> n >> X >> Y; for (int i = 1; i < n+1; i++){ int x, y; scanf("%d %d", &x, &y); po[i-1].i = i; po[i-1].dist = pow(X-x, 2)+pow(Y-y, 2); } sort(po, po+n, cmp); for (int i = 0; i < 3; i++){ printf("%d\n", po[i].i); } return 0; }