알고리즘/일반(단순구현)

[백준] 11650번 좌표 정렬하기 #Java

VIPeveloper 2022. 5. 7. 13:57
728x90
반응형
class Point implements Comparable<Point>{
    int x,y;

    public 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;
        }
        return this.x - o.x;
    }

    @Override
    public String toString() {
        return x+" " + y;
    }
}

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());

        Point[] arr = new Point[N];
        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine()," ");
            int n = Integer.parseInt(st.nextToken());
            int m = Integer.parseInt(st.nextToken());
            arr[i] = new Point(n,m);
        }

        Arrays.sort(arr);

        for (int i = 0; i < N; i++) {
            System.out.println(arr[i].toString());
        }
    }
}
728x90
반응형