티스토리 뷰

728x90

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

 

2535번: 아시아 정보올림피아드

첫 번째 줄에는 대회참가 학생 수를 나타내는 N이 주어진다. 단, 3 ≤ N ≤ 100이다. 두 번째 줄부터 N개의 줄에는 각 줄마다 한 학생의 소속 국가 번호, 학생 번호, 그리고 성적이 하나의 빈칸을 사

www.acmicpc.net


정렬을 위해 int를 Integer로 표기하고,
다중 배열을 정렬하기 위해
Arrays.sort( 배열 , new Comparator<int[]>(){}); 를 이용하여 세 번째 index에 위치한 배열값 기준으로 정렬합니다.
이 때, 올림차순으로 하려면 o1이 앞에 오게, 내림차순이면 o1이 뒤로 가게 하면 됩니다.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.*;
 
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        Integer arr[][] = new Integer[101][3];
        
        for(int i=0; i<arr.length; i++) {
            for(int j=0; j<arr[i].length; j++) {
                arr[i][j] = 0
            }
        }
        
        int nation[] = new int[101];
 
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < 3; j++) {
                arr[i][j] = sc.nextInt();
            }
        }
 
        sc.close();
 
        Arrays.sort(arr, new Comparator<Integer[]>() {
 
            @Override
            public int compare(Integer[] o1, Integer[] o2) {
                // TODO Auto-generated method stub
                return Integer.compare(o2[2], o1[2]);
            }
 
        });
 
        int res[][] = new int[3][2];
        int cnt = 0;
 
        for (int i = 0; i < N; i++) {
            if (i < 2) {
                res[cnt][0= arr[i][0];
                res[cnt][1= arr[i][1];
                cnt++;
                nation[arr[i][0]]++;
            }
            else if (nation[arr[i][0]] >= 2) {
                continue;
            } else {
                res[cnt][0= arr[i][0];
                res[cnt][1= arr[i][1];
                break;
            }
 
        }
 
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.print(res[i][j]+" ");
            }
            System.out.println();
        }
    }
}
cs

 

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

백준 11656 접미사 배열 (JAVA)  (0) 2021.08.10
백준 14490 백대열 (JAVA)  (0) 2021.08.10
백준 1205 등수 구하기 (JAVA)  (0) 2021.08.08
백준 2303 숫자 게임 (JAVA)  (0) 2021.08.04
백준 1543 문서 검색 (JAVA)  (0) 2021.08.03
댓글