작성일 :

문제 링크

28288번 - Special Event

설명

각 사람의 5일간 참석 가능 여부가 주어질 때, 참석 가능 인원이 가장 많은 날짜들의 번호를 출력하는 문제입니다.


접근법

먼저 5일에 대한 카운터 배열을 두고 각 사람의 문자열을 순회하며 Y이면 해당 날짜의 카운터를 증가시킵니다.

이후 최대값을 구하고, 그 최대값을 갖는 날짜 번호를 모아 쉼표로 이어 출력합니다.


Code

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
using System;
using System.Text;

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine()!);
    var cnt = new int[5];
    for (var i = 0; i < n; i++) {
      var s = Console.ReadLine()!;
      for (var d = 0; d < 5; d++) {
        if (s[d] == 'Y') cnt[d]++;
      }
    }

    var max = 0;
    for (var d = 0; d < 5; d++)
      max = Math.Max(max, cnt[d]);

    var sb = new StringBuilder();
    for (var d = 0; d < 5; d++) {
      if (cnt[d] == max) {
        if (sb.Length > 0) sb.Append(',');
        sb.Append(d + 1);
      }
    }
    Console.WriteLine(sb.ToString());
  }
}

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

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

  int n; cin >> n;
  int cnt[5] = {0, 0, 0, 0, 0};

  for (int i = 0; i < n; i++) {
    string s; cin >> s;
    for (int d = 0; d < 5; d++) {
      if (s[d] == 'Y') cnt[d]++;
    }
  }

  int mx = *max_element(cnt, cnt + 5);
  bool first = true;
  for (int d = 0; d < 5; d++) {
    if (cnt[d] == mx) {
      if (!first) cout << ",";
      cout << (d + 1);
      first = false;
    }
  }
  cout << "\n";

  return 0;
}

Tags: 28288, BOJ, C#, C++, 구현, 문자열, 백준, 브루트포스, 알고리즘

Categories: ,