-
Notifications
You must be signed in to change notification settings - Fork 1
/
11866.cpp
47 lines (44 loc) · 1.02 KB
/
11866.cpp
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/*
* 큐의 처음과 끝을 연결하여 원을 구현
* 큐에 아무것도 남아있지 않을 때까지 N 번째 원소를 큐에서 제거
*/
vector<int> josephusProb(int n, int k)
{
queue<int> q;
vector<int> result;
for (int i = 1; i <= n; i++) { // 큐에 N개의 원소 넣기
q.push(i);
}
int cnt = 0; // 카운트 변수
while (!q.empty()) {
int x = q.front();
q.pop();
cnt++;
if (cnt == k) { // k번째 pop하는 원소라면
result.push_back(x);
cnt = 0;
continue;
}
q.push(x); // pop한 원소 다시 push
}
return result;
}
int main()
{
int n, k;
vector<int> v;
// 입력
cin >> n >> k;
// 연산
v = josephusProb(n, k);
// 출력
cout << "<";
for (int i = 0; i < n - 1; i++) {
cout << v[i] << ", ";
}
cout << v[n - 1] << ">";
}