풀이방법
사용된 것:
정렬
Comparator Overriding
2022.04.13
Comparator를 오버라이딩하면 쉽게 풀리는 문제이다.
코드
Java(2022.04.13)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//읽어오기
int n = Integer.parseInt(br.readLine());
Point[] arr = new Point[n];
for(int i = 0; i<n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
arr[i] = new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
}
//정렬
Arrays.sort(arr, new Comparator<Point>(){
@Override
public int compare(Point o1, Point o2) {
// TODO Auto-generated method stub
if(o1.x>o2.x) return 1;
else if(o1.x == o2.x) {
if(o1.y>o2.y) return 1;
else return -1;
} else return -1;
}
});
//출력
StringBuilder sb = new StringBuilder();
for(Point p : arr) {
sb.append(p.x + " " + p.y + System.lineSeparator());
}
System.out.print(sb);
}
}
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
#4673 : 셀프 넘버 (0) | 2022.04.16 |
---|---|
#1966 : 프린터 큐 (0) | 2022.04.13 |
#10866 : 덱 (0) | 2022.04.13 |
#10845 : 큐 (0) | 2022.04.13 |
#10989 : 수 정렬하기 3 (0) | 2022.04.13 |