작성일 :

문제 링크

2975번 - Transactions

설명

거래 후 잔액을 계산해 출력하는 문제입니다.

W는 출금, D는 입금이며, 출금 후 잔액이 -200 미만이면 거래가 거부됩니다.

0 W 0이 입력되면 종료합니다.


접근법

거래 종류에 따라 잔액에서 빼거나 더합니다.

결과가 -200 미만이면 Not allowed를, 아니면 잔액을 출력합니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;

class Program {
  static void Main() {
    while (true) {
      var line = Console.ReadLine()!.Split();
      var balance = int.Parse(line[0]);
      var op = line[1];
      var amount = int.Parse(line[2]);
      if (balance == 0 && op == "W" && amount == 0) break;

      var next = (op == "W") ? balance - amount : balance + amount;
      if (next < -200) Console.WriteLine("Not allowed");
      else Console.WriteLine(next);
    }
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;

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

  while (true) {
    int bal, amt; string op;
    if (!(cin >> bal >> op >> amt)) break;
    if (bal == 0 && op == "W" && amt == 0) break;
    int next = (op == "W") ? bal - amt : bal + amt;
    if (next < -200) cout << "Not allowed\n";
    else cout << next << "\n";
  }

  return 0;
}