-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjosephus.cpp
More file actions
47 lines (37 loc) · 941 Bytes
/
josephus.cpp
File metadata and controls
47 lines (37 loc) · 941 Bytes
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 <cstdio>
#include <queue>
#include <vector>
using namespace std;
/*만약 queue를 직접 구현하고자 한다면 배열보다는 링크드리스트(vector)로 구현하는 편이 더 편리할 것이고 효율적일 것이다.*/
int main() {
queue<int> q;
int n, m;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) {
q.push(i);
}
vector<int> result;
while(!q.empty()) {
for(int i = 0; i < m; i++) {
if(i == m - 1) {
result.push_back(q.front());
q.pop();
break;
}
int temp = q.front();
q.pop();
q.push(temp);
}
}
printf("<");
for(int i = 0; i < n; i++) {
if(i == n - 1) {
printf("%d", result[i]);
break;
}
printf("%d, ", result[i]);
}
printf(">\n");
return 0;
}