알고리즘

[백준, 자바, 5635번] 생일

hminor 2024. 10. 16. 23:25
반응형

풀이

  • 2차원 배열 정렬 문제라서 한 번 풀어봄.
  • 다만 처음엔 조건식에 생일인 문자열 타입의 정수를 그대로 비교해서
  • 정렬을 했는데, 출력해보니 이상하게 정렬되어서
  • 뭐지하고 다시 정수로 형변환 후 시도 해보니
  • 원하는 대로 잘 되었다.

 

import java.io.*;
import java.util.*;
import java.util.Comparator;
public class _5635 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        int N = Integer.parseInt(br.readLine());
        Object[][] li = new Object[N][4];
        for (int i=0; i<N; i++) {
            st = new StringTokenizer(br.readLine());
            li[i][0] = st.nextToken();
            for (int j=3; j>0; j--) li[i][j] = Integer.parseInt(st.nextToken());
        }
        Arrays.sort(li, new Comparator<Object[]>() {
           public int compare(Object[] o1, Object[] o2) {
              if ((int)o1[1]!=(int)o2[1]) return Integer.compare((int)o1[1],(int)o2[1]);
              else if ((int)o1[2]!=(int)o2[2]) return Integer.compare((int)o1[2],(int)o2[2]);
              else return Integer.compare((int)o1[3],(int)o2[3]);
           }
        });
        System.out.println(li[N-1][0]);
        System.out.println(li[0][0]);
    }
}