어제 좌표정렬하기에서 compare 메서드를 조금만 수정하면 되는 문제여서
금방 풀었다!
어제 푼문제인데도 comparator 단어가 생각이 안나서 쪼끔 보고 했다ㅠㅠ
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int xy[][] = new int[n][2];
for (int i = 0; i < n; i++) {// 입력
xy[i][0] = sc.nextInt();
xy[i][1] = sc.nextInt();
}
Arrays.sort(xy, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[1] == o2[1]) {// y가 같으면
return Integer.compare(o1[0], o2[0]);// x값 비교
} else {
return Integer.compare(o1[1], o2[1]);// y값 비교
}
}
});
for (int i = 0; i < n; i++) {// 출력
System.out.println(xy[i][0] + " " + xy[i][1]);
}
}
}
'알고리즘 > 백준' 카테고리의 다른 글
백준 10814 나이순 정렬 자바 (0) | 2020.03.03 |
---|---|
백준 1181 단어 정렬 자바 (0) | 2020.02.26 |
백준 11650 좌표 정렬하기 자바 (0) | 2020.02.19 |
백준 1427 소트인사이드 자바 (0) | 2020.02.18 |
백준 2108 통계학 자바 (0) | 2020.02.12 |
댓글