Skip to content

Commit fde1759

Browse files
committed
📝 post BOJ solution
1 parent e053189 commit fde1759

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
layout: single
3+
title: "[백준 5103] DVDs (C#, C++) - soo:bak"
4+
date: "2026-01-17 20:20:00 +0900"
5+
description: "백준 5103번 C#, C++ 풀이 - DVD 재고에 대한 판매와 입고 거래를 처리해 최종 재고를 구하는 문제"
6+
tags:
7+
- 백준
8+
- BOJ
9+
- 5103
10+
- C#
11+
- C++
12+
- 알고리즘
13+
- 구현
14+
- 시뮬레이션
15+
keywords: "백준 5103, 백준 5103번, BOJ 5103, DVDs, C# 풀이, C++ 풀이, 알고리즘"
16+
---
17+
18+
## 문제 링크
19+
[5103번 - DVDs](https://www.acmicpc.net/problem/5103)
20+
21+
## 설명
22+
여러 DVD에 대한 재고 정보와 거래 내역이 주어질 때, 모든 거래를 처리한 후 각 DVD의 최종 재고를 구하는 문제입니다.
23+
24+
판매 시 재고보다 많이 팔면 가진 만큼만 차감하고, 입고 시 최대 재고를 넘는 부분은 버립니다.
25+
26+
<br>
27+
28+
## 접근법
29+
먼저 각 DVD의 코드와 초기 재고 정보를 읽으며 각 거래를 순서대로 처리합니다.
30+
31+
이 때, 판매 거래는 재고에서 판매량을 빼되 0 이하로 내려가지 않도록 하고, 입고 거래는 재고에 입고량을 더하되 최대 재고를 넘지 않도록 합니다.
32+
33+
모든 거래를 처리한 후 DVD 코드와 최종 재고를 출력합니다.
34+
35+
<br>
36+
37+
- - -
38+
39+
## Code
40+
41+
### C#
42+
```csharp
43+
using System;
44+
45+
class Program {
46+
static void Main() {
47+
while (true) {
48+
var code = Console.ReadLine();
49+
if (code == null || code == "#")
50+
break;
51+
52+
var ms = Console.ReadLine()!.Split();
53+
var m = int.Parse(ms[0]);
54+
var s = int.Parse(ms[1]);
55+
56+
var t = int.Parse(Console.ReadLine()!);
57+
for (var i = 0; i < t; i++) {
58+
var parts = Console.ReadLine()!.Split();
59+
var type = parts[0][0];
60+
var x = int.Parse(parts[1]);
61+
if (type == 'S')
62+
s = Math.Max(0, s - x);
63+
else
64+
s = Math.Min(m, s + x);
65+
}
66+
Console.WriteLine($"{code} {s}");
67+
}
68+
}
69+
}
70+
```
71+
72+
### C++
73+
```cpp
74+
#include <bits/stdc++.h>
75+
using namespace std;
76+
77+
int main() {
78+
ios::sync_with_stdio(false);
79+
cin.tie(nullptr);
80+
81+
while (true) {
82+
string code;
83+
if (!(cin >> code))
84+
break;
85+
if (code == "#")
86+
break;
87+
88+
int m, s; cin >> m >> s;
89+
int t; cin >> t;
90+
for (int i = 0; i < t; i++) {
91+
char type; int x; cin >> type >> x;
92+
if (type == 'S')
93+
s = max(0, s - x);
94+
else
95+
s = min(m, s + x);
96+
}
97+
cout << code << " " << s << "\n";
98+
}
99+
100+
return 0;
101+
}
102+
```

0 commit comments

Comments
 (0)