Algorithm/백준
[백준] 11866 요세푸스 문제 0 with C++
nowkoes
2023. 5. 15. 00:00
문제설명
입출력 예제
개념
예제 (7,3) 요세푸스 순열이 생성되는 과정은 다음과 같다.
따라서 K번 만큼 수를 뒤로 보낸 후 수를 뽑으면 요세푸스 순열이 생성된다. 즉, 선입선출의 구조를 갖고 있으므로 큐 자료구조를 활용하면 쉽게 풀 수 있다.
풀이
#include <iostream>
#include <queue>
using namespace std;
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
int N, K;
cin >> N >> K;
queue<int> q;
for (int i = 1; i <= N; ++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();
if (q.size() != 1)
cout << ", ";
q.pop();
}
cout << ">";
}
반응형