작성일 :

문제 링크

30552번 - Attendance

설명

이름 호출과 Present! 응답이 주어질 때, 출석 응답이 없는 학생들의 이름을 순서대로 출력하는 문제입니다.


접근법

모든 호출을 문자열로 저장한 뒤, 각 이름 다음 줄이 Present!인지 확인합니다. 다음 줄이 Present!이면 출석으로 처리하고, 그렇지 않으면 결석이므로 결과에 추가합니다. 결과가 없으면 No Absences를 출력합니다.


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

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine()!);
    var lines = new List<string>();
    for (var i = 0; i < n; i++)
      lines.Add(Console.ReadLine()!);

    var absents = new List<string>();
    for (var i = 0; i < n; i++) {
      var line = lines[i];
      if (line == "Present!") continue;
      if (i + 1 < n && lines[i + 1] == "Present!") continue;
      absents.Add(line);
    }

    if (absents.Count == 0) Console.WriteLine("No Absences");
    else {
      foreach (var name in absents)
        Console.WriteLine(name);
    }
  }
}

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;

typedef vector<string> vs;

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

  int n; cin >> n;
  cin.ignore();
  vs lines(n);
  for (int i = 0; i < n; i++)
    getline(cin, lines[i]);

  vs absents;
  for (int i = 0; i < n; i++) {
    if (lines[i] == "Present!") continue;
    if (i + 1 < n && lines[i + 1] == "Present!") continue;
    absents.push_back(lines[i]);
  }

  if (absents.empty()) cout << "No Absences\n";
  else {
    for (const auto& name : absents)
      cout << name << "\n";
  }

  return 0;
}