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 << ">";
}

 

반응형

'Algorithm > 백준' 카테고리의 다른 글

[백준] 10866 덱 with C++  (0) 2023.05.17
[백준] 1966 프린터 큐 with C++  (0) 2023.05.16
[백준] 18258 큐 2 with C++  (2) 2023.05.14
[백준] 2164 카드2 with C++  (0) 2023.05.13
[백준] 1874 스택 수열 with C++  (0) 2023.05.12