728x90
원형 큐 문제입니다.
큐에 값을 전부 넣고
K - 1까지의 값을 맨뒤로 넣습니다.
그렇게 되면 K번째의 수를 찾을 수 있고
K번째 수를 pop해줍니다.
이를 반복합니다.
#include <iostream>
#include <queue>
using namespace std;
int main(int argc, char *argv[])
{
int N, K;
queue<int> q;
cin >> N >> K;
for (int i = 1; N >= i; i++) {
q.push(i);
}
cout << "<";
while (!q.empty()) {
for (int i = 0; i < K - 1; i++) {
q.push(q.front());
q.pop();
}
cout << q.front();
q.pop();
if (!q.empty()) {
cout << ", ";
}
}
cout << ">" << endl;
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 10866번 덱 (0) | 2019.09.08 |
---|---|
[C++] 백준 1966번 프린터 큐 (0) | 2019.09.08 |
[C++] 백준 10845번 큐 (0) | 2019.09.07 |
[C++] 백준 17298번 오큰수 (2) | 2019.09.05 |
[C++] 백준 1874번 스택 수열 (0) | 2019.09.05 |