202009-1 稱檢測點查詢(Java 100)

山淼發表於2020-10-13
  • 問題連結稱檢測點查詢

  • 問題分析:模擬法解決。

  • 程式說明:n表示檢測點的個數;X和Y表示市民所在位置

  • 程式程式碼

import java.util.Arrays;
import java.util.Scanner;

class Area implements Comparable{
	public int x;
	public int y;
	public int code;
	public int distance;
	Area(int x1,int y1,int c,int d){
		this.x = x1;
		this.y = y1;
		this.code = c;
		this.distance = d;
	}
	@Override
	public int compareTo(Object o) {//排序,首要按照距離從小到大排序,若距離相等,按照編號從小到大排序
		Area area = (Area)o;
		if(this.distance!=area.distance)
			return this.distance-area.distance;
		return this.code-area.code;
	}
	
}
public class Main {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int n = reader.nextInt();
		int X = reader.nextInt();
		int Y = reader.nextInt();
		Area[] areas = new Area[n];
		for(int i=0;i<n;i++) {
			int x1 = reader.nextInt();
			int y1 = reader.nextInt();
			int dis = (x1-X)*(x1-X)+(y1-Y)*(y1-Y);
			areas[i] = new Area(x1,y1,i+1,dis);
		}
		Arrays.sort(areas);
		System.out.println(areas[0].code);
		System.out.println(areas[1].code);
		System.out.println(areas[2].code);
	}

}

相關文章