[백준_11651] 좌표 정렬하기 2 - JAVA

jm_25·2021년 12월 6일
0

알고리즘

목록 보기
26/40

문제 출처

https://www.acmicpc.net/problem/11651

풀이

  • 기존에 풀었던 11650 좌표 정렬하기와 동일한 유형의 문제이다.
  • 기존 코드의 비교 방식에서 x, y 만 변경해주면 된다.

코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

public class Main {
    public static class Coordinate {
        int x;
        int y;

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

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine());
        int N = Integer.parseInt(stringTokenizer.nextToken());
        List<Coordinate> coordinateList = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            stringTokenizer = new StringTokenizer(bufferedReader.readLine());
            coordinateList.add(new Coordinate(Integer.parseInt(stringTokenizer.nextToken()), Integer.parseInt(stringTokenizer.nextToken())));
        }
        Collections.sort(coordinateList, (o1, o2) -> {
            if (o1.y == o2.y) {
                return o1.x - o2.x;
            } else {
                return o1.y - o2.y;
            }
        });
        for (Coordinate coordinate : coordinateList) {
            System.out.println(coordinate.x + " " + coordinate.y);
        }
    }
}

채점 결과

profile
매일 매일 한 개씩

0개의 댓글