Post

[BaekJoon] 35306번 - 월간 향유회 시즌 종료 [Java][C++]

[BaekJoon] 35306번 - 월간 향유회 시즌 종료 [Java][C++]

문제 링크


1. 문제 풀이


각 운영진은 $K$ 개의 평가 지표가 있고 특정 평가 지표가 다른 모든 운영진 보다 높으면 수장 후보가 된다. 2차원 배열을 활용해서 입력을 받은 후 각 열에서 가장 큰 수가 하나만 존재할 경우 해당 수가 포함된 행을 체크해주고 이후 개수를 세주면 된다.


2. 코드


1. 풀이 [Java]

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
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));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int n = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());

        int[][] arr = new int[n][k];
        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < k; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        boolean[] check = new boolean[n];
        for (int i = 0; i < k; i++) {
            int max = 0;
            int cnt = 0;
            int idx = 0;

            for (int j = 0; j < n; j++) {
                if (arr[j][i] > max) {
                    max = arr[j][i];
                    cnt = 1;
                    idx = j;
                } else if (arr[j][i] == max) {
                    cnt++;
                }
            }

            if (cnt == 1) {
                check[idx] = true;
            }
        }

        int cnt = 0;
        for (boolean flag : check) {
            if (flag) cnt++;
        }

        System.out.println(cnt);
    }
}


2. 풀이 [C++]

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
#include <bits/stdc++.h>
using namespace std;

int arr[2000][2000];
bool check[2000];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < k; j++) {
            cin >> arr[i][j];
        }
    }

    for (int i = 0; i < k; i++) {
        int mx = 0;
        int cnt = 0;
        int idx = 0;

        for (int j = 0; j < n; j++) {
            if (arr[j][i] > mx) {
                mx = arr[j][i];
                cnt = 1;
                idx = j;
            } else if (arr[j][i] == mx) {
                cnt++;
            }
        }

        if (cnt == 1) {
            check[idx] = true;
        }
    }

    int cnt = 0;
    for (int i = 0; i < n; i++) {
        if (check[i]) cnt++;
    }

    cout << cnt;
}

This post is licensed under CC BY 4.0 by the author.