ccf202009-1称检测点查询

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int x = scanner.nextInt();
        int y = scanner.nextInt();

        List<CheckPoint> pointList = new ArrayList<>();
        int no = 1;
        for(int i = 0; i < num; i++){
            int x1 = scanner.nextInt();
            int y1 = scanner.nextInt();

            CheckPoint tmp = new CheckPoint(x1,y1,no++);
            pointList.add(tmp);
        }

        for(CheckPoint checkPoint : pointList){
            checkPoint.distance = checkPoint.getDistance(x,y);
        }

        Collections.sort(pointList);
        int count = 0;
        for(CheckPoint checkPoint : pointList){
            if(count >= 3){
                break;
            }
            System.out.println(checkPoint.no);
            count++;
        }

    }
}



class CheckPoint implements Comparable<CheckPoint>{
    int x;
    int y;
    int no;
    double distance;

    public CheckPoint(int x, int y, int no) {
        this.x = x;
        this.y = y;
        this.no = no;
    }

    public double getDistance(int x, int y){
        return Math.sqrt(Math.pow(this.x - x,2) + Math.pow(this.y - y, 2));
    }

    @Override
    public int compareTo(CheckPoint o) {
        if(this.distance - o.distance > 0){
            return 1;
        }
        else if(this.distance - o.distance < 0)
        {
            return -1;
        }
        else {
            return this.no - o.no;
        }
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值