알고리즘/백준
백준 11651 좌표 정렬하기 2 자바
reumiii
2020. 2. 20. 22:48
어제 좌표정렬하기에서 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]);
}
}
}