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

题目链接:

计算机软件能力认证考试系统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> {
        @Override
        public 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);
        }
    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值