알고리즘/BOJ

[C++] 백준 11866번 요세푸스 문제 0

Jay, Lee 2021. 4. 4. 20:27

처음에 벡터로 풀었다가

알고리즘 분류쪽에 '큐'라고 나와있어서

다시 푼 문제

 

간단하게 앞에거 거를 k-1만큼 뒤로 붙이면서 pop 시켜버리면

맨 앞에 것이 k번째 수가 되니까

그거만 pop하는 과정을 반복하면 된다.

#include <iostream>
#include <queue>
using namespace std;

int main() {

	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);

	int n, k;
	queue<int> q;
	cin >> n >> k;

	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();
		q.pop();

		if (!q.empty()) {
			cout << ", ";
		}
	}
	cout << ">" << "\n";


	return 0;
}