BOJ - 11650

아이모·2022년 11월 21일
0

BOJ 길라잡이

목록 보기
3/9

1. 문제

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

2. 풀이

BOJ - 1181과 똑같이 Comparator를 사용하는 문제이다.
1181과 다른 점이라면 좌표를 나타내기 위한 클래스를 추가로 생성하는 정도이다.


import java.util.*;
import java.io.*;


public class Main11650 {
    static class Point{
        public int point_x;
        public int point_y;

        Point(int x, int y){
            point_x = x;
            point_y = y;
        }
    }
    public static void main(String [] args){

        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        Point [] pointArr = new Point[N];
        for(int i = 0; i < N; i++){
            int x =  sc.nextInt();
            int y = sc.nextInt();
            pointArr[i] = new Point(x, y);
        }
        Arrays.sort(pointArr, new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                if(o1.point_x == o2.point_x){
                    return o1.point_y - o2.point_y;
                }
                else{
                    return o1.point_x - o2.point_x;
                }

            }
        });

        for(Point p : pointArr){
            System.out.println(p.point_x + " " + p.point_y);
        }

    }
}

3. 적용된 알고리즘

정렬

profile
데이터로 보는 실력

0개의 댓글