반응형
백준 정렬 문제이고 비교해야 할 게 좀 있다보니 Comparator를 씁니다.
Arrays.sort(birth, new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
if(o1[3].equals(o2[3])) {
if(o1[2].equals(o2[2])) {
return Integer.parseInt(o1[1]) - Integer.parseInt(o2[1]);
}
return Integer.parseInt(o1[2]) - Integer.parseInt(o2[2]);
}
return Integer.parseInt(o1[3]) - Integer.parseInt(o2[3]);
}
});
o1[3] = 연도
o1[2] = 월
o1[1] = 일
연도가 같다면 연도 오름차순
연도 같고 월이 같다면 월 오름차순
연도 같고 월도 같다면 일 오름차순
으로 정렬합니다.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String[][] birth = new String[N][4];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
birth[i][0] = st.nextToken();
birth[i][1] = st.nextToken();
birth[i][2] = st.nextToken();
birth[i][3] = st.nextToken();
}
Arrays.sort(birth, new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
if(o1[3].equals(o2[3])) {
if(o1[2].equals(o2[2])) {
return Integer.parseInt(o1[1]) - Integer.parseInt(o2[1]);
}
return Integer.parseInt(o1[2]) - Integer.parseInt(o2[2]);
}
return Integer.parseInt(o1[3]) - Integer.parseInt(o2[3]);
}
});
System.out.println(birth[N-1][0]);
System.out.println(birth[0][0]);
}
}
반응형
'백준 > 정렬' 카테고리의 다른 글
백준 2693 자바 (0) | 2022.10.30 |
---|---|
백준 10825 자바 (2) (0) | 2022.10.10 |
백준 1940 자바 (0) | 2022.08.26 |
백준 1269 자바 (0) | 2022.08.21 |
백준 11728 자바 (0) | 2022.08.21 |