좌표 정렬

brightvvater·2023년 3월 14일

풀이>

import java.util.*;
class Point implements Comparable<Point> {
    public int x,y;
    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    @Override
    public int compareTo(Point o) {
        if(this.x ==o.x) return this.y-o.y;
        else return this.x - o.x;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        int n = kb.nextInt();
        ArrayList<Point> arr = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int x = kb.nextInt();
            int y = kb.nextInt();
            arr.add(new Point(x, y));
        }
        Collections.sort(arr);
        for (Point o : arr) {
            System.out.println(o.x+" "+o.y);
        }
    }
}
profile
코딩을 잘하고 싶은 입문자

0개의 댓글