当前位置: 代码迷 >> 综合 >> CCF 202009-1 称检测点查询 (简单结构体排序)
  详细解决方案

CCF 202009-1 称检测点查询 (简单结构体排序)

热度:91   发布时间:2023-12-22 13:02:09.0

传送门

题意:目标就是找到 n 个点中离 (X,Y)直线距离最近的情况下编号最小的三个点,直接记录每个点与 (X,Y)的距离再结构体排序一下就好啦。

代码实现:

import java.util.*;class node implements Comparable<node>{int id, da;public node(int id, int da){this.id = id;this.da = da;}@Overridepublic int compareTo(node o) {if(this.da!=o.da)return this.da-o.da;elsereturn this.id-o.id;}
}public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);int n = in.nextInt();int x = in.nextInt();int y = in.nextInt();node [] a = new node[250];for(int i = 1; i <= n; i ++){int u = in.nextInt();int v = in.nextInt();a[i] = new node(i, (u-x)*(u-x)+(v-y)*(v-y));}Arrays.sort(a, 1, n+1);for(int i = 1; i <= 3; i ++)System.out.println(a[i].id);in.close();}
}