T_era

[JAVA] 백준 11650 : 좌표정렬 본문

Programing/BaekJoon

[JAVA] 백준 11650 : 좌표정렬

블스뜸 2025. 3. 29. 15:38
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        Coordinate[] cord = new Coordinate[Integer.parseInt(br.readLine())];
        StringTokenizer st;

        for (int i = 0; i < cord.length; i++) {
            st = new StringTokenizer(br.readLine());
            cord[i] = new Coordinate(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
        }

        Arrays.sort(cord, new Comparator<Coordinate>() {
            @Override
            public int compare(Coordinate o1, Coordinate o2) {
                if (o1.x == o2.x) {
                    return o1.y - o2.y;
                }
                return o1.x - o2.x;
            }
        });

        StringBuilder sb = new StringBuilder();
        for (Coordinate item : cord) {
            sb.append(item.x).append(" ").append(item.y).append("\n");
        }

        bw.write(sb.toString());
        bw.flush();
        bw.close();
        br.close();
    }
}

class Coordinate {
    int x;
    int y;

    Coordinate(int x, int y) {
        this.x = x;
        this.y = y;
    }
}