Skip to content

Commit 22ecfc0

Browse files
committed
📝 Post BOJ Solution.
1 parent d8e6640 commit 22ecfc0

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
layout: single
3+
title: "[백준 10845] 큐 (C#, C++) - soo:bak"
4+
date: "2024-12-13 04:16:00 +0900"
5+
description: 자료 구조, 큐를 활용한 백준 10845번 문제를 C#과 C++로 풀이 및 해설
6+
---
7+
8+
## 문제 링크
9+
[10845번 - 큐](https://www.acmicpc.net/problem/10845)
10+
11+
## 설명
12+
정수를 저장하는 **큐(Queue)**를 구현하고 다양한 명령을 처리하는 문제입니다.<br>
13+
<br>
14+
15+
### 문제 이해
16+
다음 6가지 명령을 처리해야 합니다:<br>
17+
1. **push X**: 정수 `X`를 큐에 넣습니다.<br>
18+
2. **pop**: 큐에서 가장 앞에 있는 정수를 빼고 출력합니다. 큐가 비어있으면 `-1`을 출력합니다.<br>
19+
3. **size**: 큐에 들어있는 정수의 개수를 출력합니다.<br>
20+
4. **empty**: 큐가 비어있으면 `1`, 아니면 `0`을 출력합니다.<br>
21+
5. **front**: 큐의 가장 앞에 있는 정수를 출력합니다. 큐가 비어있으면 `-1`을 출력합니다.<br>
22+
6. **back**: 큐의 가장 뒤에 있는 정수를 출력합니다. 큐가 비어있으면 `-1`을 출력합니다.<br>
23+
24+
<br>
25+
간단하게 배열을 활용하여 큐를 구현해봅니다. <br>
26+
<br>
27+
28+
- - -
29+
30+
## Code
31+
<b>[ C# ] </b>
32+
<br>
33+
34+
```csharp
35+
using System.Text;
36+
37+
namespace Solution {
38+
class MyQueue {
39+
private int[] data;
40+
private int front;
41+
private int back;
42+
43+
public MyQueue(int size) {
44+
data = new int[size];
45+
front = 0;
46+
back = 0;
47+
}
48+
49+
public void Push(int value) => data[back++] = value;
50+
51+
public int Pop() => front == back ? -1 : data[front++];
52+
53+
public int Size() => back - front;
54+
55+
public int Empty() => front == back ? 1 : 0;
56+
57+
public int Front() => front == back ? -1 : data[front];
58+
59+
public int Back() => front == back ? -1 : data[back - 1];
60+
}
61+
62+
class Program {
63+
static void Main(string[] args) {
64+
65+
var sb = new StringBuilder();
66+
67+
var n = int.Parse(Console.ReadLine()!);
68+
var queue = new MyQueue(n);
69+
for (int i = 0; i < n; i++) {
70+
var input = Console.ReadLine()!.Split();
71+
var cmd = input[0];
72+
73+
if (cmd == "push") queue.Push(int.Parse(input[1]));
74+
else if (cmd == "pop") sb.AppendLine(queue.Pop().ToString());
75+
else if (cmd == "size") sb.AppendLine(queue.Size().ToString());
76+
else if (cmd == "empty") sb.AppendLine(queue.Empty().ToString());
77+
else if (cmd == "front") sb.AppendLine(queue.Front().ToString());
78+
else if (cmd == "back") sb.AppendLine(queue.Back().ToString());
79+
}
80+
81+
Console.Write(sb);
82+
}
83+
}
84+
}
85+
```
86+
<br><br>
87+
<b>[ C++ ] </b>
88+
<br>
89+
90+
```cpp
91+
#include <bits/stdc++.h>
92+
#define MAX 10001
93+
94+
using namespace std;
95+
96+
class MyQueue {
97+
int data[MAX];
98+
int front;
99+
int back;
100+
101+
public:
102+
MyQueue() {
103+
front = 0;
104+
back = 0;
105+
}
106+
107+
void push(int value) { data[back++] = value; }
108+
109+
int pop() {
110+
if (front == back) return -1;
111+
return data[front++];
112+
}
113+
114+
int size() { return back - front; }
115+
116+
int empty() { return front == back ? 1 : 0; }
117+
118+
int getFront() {
119+
if (front == back) return -1;
120+
return data[front];
121+
}
122+
123+
int getBack() {
124+
if (front == back) return -1;
125+
return data[back - 1];
126+
}
127+
};
128+
129+
int main() {
130+
ios::sync_with_stdio(false);
131+
cin.tie(nullptr);
132+
133+
int n; cin >> n;
134+
135+
MyQueue queue;
136+
for (int i = 0; i < n; i++) {
137+
string cmd; cin >> cmd;
138+
139+
if (cmd == "push") {
140+
int num; cin >> num;
141+
queue.push(num);
142+
} else if (cmd == "pop") cout << queue.pop() << "\n";
143+
else if (cmd == "size") cout << queue.size() << "\n";
144+
else if (cmd == "empty") cout << queue.empty() << "\n";
145+
else if (cmd == "front") cout << queue.getFront() << "\n";
146+
else if (cmd == "back") cout << queue.getBack() << "\n";
147+
}
148+
149+
return 0;
150+
}
151+
```

0 commit comments

Comments
 (0)