[백준 5103] DVDs (C#, C++) - soo:bak
작성일 :
문제 링크
설명
여러 DVD에 대한 재고 정보와 거래 내역이 주어질 때, 모든 거래를 처리한 후 각 DVD의 최종 재고를 구하는 문제입니다.
판매 시 재고보다 많이 팔면 가진 만큼만 차감하고, 입고 시 최대 재고를 넘는 부분은 버립니다.
접근법
먼저 각 DVD의 코드와 초기 재고 정보를 읽으며 각 거래를 순서대로 처리합니다.
이 때, 판매 거래는 재고에서 판매량을 빼되 0 이하로 내려가지 않도록 하고, 입고 거래는 재고에 입고량을 더하되 최대 재고를 넘지 않도록 합니다.
모든 거래를 처리한 후 DVD 코드와 최종 재고를 출력합니다.
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
using System;
class Program {
static void Main() {
while (true) {
var code = Console.ReadLine();
if (code == null || code == "#")
break;
var ms = Console.ReadLine()!.Split();
var m = int.Parse(ms[0]);
var s = int.Parse(ms[1]);
var t = int.Parse(Console.ReadLine()!);
for (var i = 0; i < t; i++) {
var parts = Console.ReadLine()!.Split();
var type = parts[0][0];
var x = int.Parse(parts[1]);
if (type == 'S')
s = Math.Max(0, s - x);
else
s = Math.Min(m, s + x);
}
Console.WriteLine($"{code} {s}");
}
}
}
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
while (true) {
string code;
if (!(cin >> code))
break;
if (code == "#")
break;
int m, s; cin >> m >> s;
int t; cin >> t;
for (int i = 0; i < t; i++) {
char type; int x; cin >> type >> x;
if (type == 'S')
s = max(0, s - x);
else
s = min(m, s + x);
}
cout << code << " " << s << "\n";
}
return 0;
}