작성일 :

문제 링크

14535번 - Birthday Graph

설명

여러 명의 생일 날짜가 주어지면 월별 빈도를 세어 그래프 형태로 출력합니다.

각 테스트케이스에 대해 12개월을 1월부터 12월 순으로 해당 월의 별표 개수만큼 출력합니다.


접근법

먼저, 월 이름 배열을 준비하고 각 테스트케이스마다 크기 12의 빈도 배열을 초기화합니다.

다음으로, N개의 생일을 읽어 해당 월의 빈도를 증가시킵니다. 월은 1부터 시작하므로 1을 빼서 인덱스로 사용합니다.

이후, 각 월에 대해 월 이름과 빈도만큼의 별표를 출력합니다.



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
29
30
using System;

namespace Solution {
  class Program {
    static void Main(string[] args) {
      var months = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
      var tc = 1;
      while (true) {
        var line = Console.ReadLine();
        if (line == null)
          break;
        var n = int.Parse(line);
        if (n == 0)
          break;

        var cnt = new int[12];
        for (var i = 0; i < n; i++) {
          var parts = Console.ReadLine()!.Split();
          var month = int.Parse(parts[1]);
          cnt[month - 1]++;
        }

        Console.WriteLine($"Case #{tc}:");
        for (var i = 0; i < 12; i++)
          Console.WriteLine($"{months[i]}:{new string('*', cnt[i])}");
        tc++;
      }
    }
  }
}

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

typedef vector<string> vs;

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

  vs mon = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  int n, tc = 1;
  while (cin >> n) {
    if (n == 0)
      break;
    int cnt[12] = {0, };
    for (int i = 0; i < n; i++) {
      int d, m, y; cin >> d >> m >> y;
      cnt[m - 1]++;
    }
    cout << "Case #" << tc << ":\n";
    for (int i = 0; i < 12; i++)
      cout << mon[i] << ":" << string(cnt[i], '*') << "\n";
    tc++;
  }

  return 0;
}