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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
namespace 연습장
{
internal class Program
{
static void Main(string[] args)
{
using var reader = new StreamReader(Console.OpenStandardInput());
using var print = new StreamWriter(Console.OpenStandardOutput());
_2164_boj boj = new _2164_boj();
boj.boj_2164();
}
}
internal class _2164_boj
{
static int n;
static int front;
static int rear;
static string[] queue;
public void boj_2164()
{
n = int.Parse(Console.ReadLine());
queue = new string[n + 1];
front = 0;
rear = 0;
for (int i = 1; i <= n; i++)
enqueue(i);
int cnt = n;
while (true)
{
if (cnt == 1)
break;
dequeue();
cnt--;
enqueue(int.Parse(dequeue()));
}
Console.WriteLine(dequeue());
}
public bool isQueueFull()
{
if ((rear + 1) % (n + 1) == front)
return true;
else
return false;
}
public bool isQueueEmpty()
{
if (rear == front)
return true;
else
return false;
}
public void enqueue(int data)
{
if (isQueueFull())
return;
rear = (rear + 1) % (n + 1);
queue[rear] = data.ToString();
}
public string dequeue()
{
string data;
if (isQueueEmpty())
return null;
front = (front + 1) % (n + 1);
data = queue[front];
queue[front] = null;
return data;
}
}
}
|
cs |
수업 시간에 큐를 배운만큼 큐의 메서드들을 직접 구현해 보았다.
문제가 덱으로 풀면 참 쉽게 풀릴거 같은데 나는 덱을 안배웠으니 내가 배운 원형 큐를 이용해서 풀어보았다.
'하루 한 접시' 카테고리의 다른 글
[백준] 1874번 : 스택 수열 [C#] (0) | 2024.05.16 |
---|---|
[백준] 요세푸스 문제 0 [C#] (0) | 2024.05.14 |
[백준] 1759번: 암호 만들기 [C#] (0) | 2024.05.13 |
[백준] 10828번: 스택 [C#] (0) | 2024.05.12 |
[백준] 8979번: 올림픽 [C#] (0) | 2024.05.08 |