작성일 :

문제 링크

7598번 - Bookings

설명

항공편마다 예약과 취소 내역이 주어질 때 최종 예약 좌석 수를 출력하는 문제입니다.


접근법

먼저 항공편 번호와 현재 좌석 수를 읽고 시나리오를 시작합니다.

다음으로 예약은 좌석 상한을 넘지 않을 때만 반영하고, 취소는 현재 좌석 수 이하일 때만 반영합니다.

이후 X 0이 나오면 해당 항공편을 끝내고 결과를 출력합니다.

마지막으로 # 0이 나올 때까지 모든 항공편을 처리합니다.



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

class Program {
  static void Main() {
    var sb = new StringBuilder();
    while (true) {
      var line = Console.ReadLine();
      if (line == null) break;
      if (line == "# 0") break;
      if (line.Length == 0) continue;

      var parts = line.Split();
      var flight = parts[0];
      var booked = int.Parse(parts[1]);

      while (true) {
        var t = Console.ReadLine()!;
        var p = t.Split();
        var op = p[0];
        var n = int.Parse(p[1]);
        if (op == "X") break;
        if (op == "B") {
          if (booked + n <= 68) booked += n;
        } else if (n <= booked) booked -= n;
      }

      sb.Append(flight).Append(' ').Append(booked).AppendLine();
    }

    Console.Write(sb);
  }
}

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

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

  string flight;
  int booked;
  while (cin >> flight >> booked) {
    if (flight == "#" && booked == 0) break;
    while (true) {
      string op; int n;
      cin >> op >> n;
      if (op == "X") break;
      if (op == "B") {
        if (booked + n <= 68) booked += n;
      } else if (n <= booked) booked -= n;
    }
    cout << flight << " " << booked << "\n";
  }

  return 0;
}