작성일 :

문제 링크

1362번 - 펫

설명

시나리오별로 펫의 체중 변화를 적용해 최종 상태를 출력하는 문제입니다.


접근법

명령을 순서대로 처리하며 체중을 갱신하고, 체중이 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
34
using System;

class Program {
  static void Main() {
    var idx = 1;
    while (true) {
      var first = Console.ReadLine()!.Split();
      var o = int.Parse(first[0]);
      var w = int.Parse(first[1]);
      if (o == 0 && w == 0) break;

      var dead = false;
      while (true) {
        var parts = Console.ReadLine()!.Split();
        var cmd = parts[0];
        var n = int.Parse(parts[1]);
        if (cmd == "#" && n == 0) break;

        if (dead) continue;
        if (cmd == "E") w -= n;
        else w += n;
        if (w <= 0) dead = true;
      }

      string state;
      if (dead || w <= 0) state = "RIP";
      else if (w > o / 2.0 && w < o * 2) state = ":-)";
      else state = ":-(";

      Console.WriteLine($"{idx} {state}");
      idx++;
    }
  }
}

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

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

  int idx = 1;
  while (true) {
    int o, w; cin >> o >> w;
    if (o == 0 && w == 0) break;

    bool dead = false;
    while (true) {
      string cmd; int n; cin >> cmd >> n;
      if (cmd == "#" && n == 0) break;

      if (dead) continue;
      if (cmd == "E") w -= n;
      else w += n;
      if (w <= 0) dead = true;
    }

    string state;
    if (dead || w <= 0) state = "RIP";
    else if (w > o / 2.0 && w < o * 2) state = ":-)";
    else state = ":-(";

    cout << idx << " " << state << "\n";
    idx++;
  }

  return 0;
}