티스토리 뷰

코딩/자바 백준

백준 5635 생일 (JAVA)

박상어 2021. 8. 21. 23:28
728x90

https://www.acmicpc.net/problem/5635

 

5635번: 생일

어떤 반에 있는 학생들의 생일이 주어졌을 때, 가장 나이가 적은 사람과 가장 많은 사람을 구하는 프로그램을 작성하시오.

www.acmicpc.net

 

Arrays.sort( 배열명, new Comparator<int[]>(){}) 을 이용하여 풀었습니다.

나이가 많은 순으로 정렬을 하여 첫번째 값과 끝 값을 출력하면 되는 쉬운 문제였습니다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.*;
 
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        String arr[][] = new String[N][4];
 
        for (int i = 0; i < N; i++) {
            arr[i][0= sc.next();
            arr[i][1= sc.next();
            arr[i][2= sc.next();
            arr[i][3= sc.next();
        }
 
        Arrays.sort(arr, new Comparator<String[]>() {
 
            @Override
            public int compare(String[] o1, String[] o2) {
                if (Integer.parseInt(o1[3]) == Integer.parseInt(o2[3])) {
                    if (Integer.parseInt(o1[2]) == Integer.parseInt(o2[2])) {
                        return Integer.compare(Integer.parseInt(o1[1]), Integer.parseInt(o2[1]));
                    } else
                        return Integer.compare(Integer.parseInt(o1[2]), Integer.parseInt(o2[2]));
                }
                return Integer.compare(Integer.parseInt(o1[3]), Integer.parseInt(o2[3]));
            }
        });
 
        System.out.println(arr[N-1][0]+"\n"+arr[0][0]);
 
        sc.close();
    }
}
 
cs

 

'코딩 > 자바 백준' 카테고리의 다른 글

백준 6603 로또 (JAVA)  (0) 2021.08.22
백준 2822 점수 계산 (JAVA)  (0) 2021.08.21
백준 17413 단어 뒤집기2 (JAVA)  (0) 2021.08.20
백준 5525 IOIOI (JAVA)  (0) 2021.08.20
백준 2607 비슷한 단어(JAVA)  (0) 2021.08.19
댓글