PS/BOJ

[백준] 1158. 요세푸스 문제(c++)

backend 개발자 지망생 2025. 1. 26. 23:11

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

문제를 보고 queue가 떠올라서 queue로 풀었지만, 연결리스트로 풀어도 똑같다고 생각한다.

 


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

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n, m, tmp;

	cin >> n >> m;

	queue<int> q;

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


	cout << '<';
	
	while (q.size() > 1) {
		for (int i = 0; i < m - 1; i++) {
			tmp = q.front();
			q.push(tmp);
			q.pop();
		}

		tmp = q.front();
		cout << tmp << ", ";
		q.pop();
	}

	tmp = q.front();

	cout << tmp << ">\n";

	return 0;
}