카테고리 없음

백준 1158번 요세푸스문제

뇌장하드 2021. 11. 4. 21:07

https://www.acmicpc.net/problem/1158

 

1158번: 요세푸스 문제

첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 5,000)

www.acmicpc.net

 

 

K번쨰사람이 제거해야하니 K-1까지는 넘어가고 K번쨰는 pop()시킨다.

 

소스코드

#include<bits/stdc++.h>

using namespace std;

int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, t;
	int temp;
	queue<int> q;

	cin >> n >> t;

	for (int i = 1; i <= n; ++i) {
		q.push(i);
	}

	cout << "<";
	while (1) {
		for (int i = 0; i < t - 1; ++i) {
			temp = q.front();
			q.pop();
			q.push(temp);
		}
		cout << q.front();
		q.pop();
		if (!q.size()) {
			break;
		}
		cout << ", ";
	}
	cout << ">";

	return 0;
}