当前位置: 代码迷 >> 综合 >> CSP 202009-1 称检测点查询(Java)
  详细解决方案

CSP 202009-1 称检测点查询(Java)

热度:63   发布时间:2023-12-22 07:42:32.0

题目链接:

计算机软件能力认证考试系统icon-default.png?t=M276http://118.190.20.162/view.page?gpid=T113

【分析】计算距离,然后用一个Pair存储,按照要求排序即可。

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;public class Main {public static class Pair{int d;int i;public Pair(int d, int i){this.d = d;this.i = i;}}static class cmp implements Comparator<Pair> {@Overridepublic int compare(Pair o1, Pair o2){if(o1.d < o2.d || (o1.d == o2.d && o1.i < o2.i)) return -1;return 1;}}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n, x, y, a, b;n = scanner.nextInt();x = scanner.nextInt();y = scanner.nextInt();int i, j;List<Pair> list = new ArrayList<>();for(i = 0; i < n; i++){a = scanner.nextInt();b = scanner.nextInt();list.add(new Pair((a - x) * (a - x) + (b - y) * (b - y), i + 1));}list.sort(new cmp());
//        for(Pair pair: list){
//            System.out.print(pair.i);
//            System.out.print(" ");
//            System.out.print(pair.d);
//            System.out.println();
//        }for(i = 0; i < 3; i++){System.out.println(list.get(i).i);}}}

 

 

  相关解决方案