개발일지/알고리즘

백준 1158번 : 요세푸스 문제

김진우 개발일지 2025. 1. 25. 20:51
#include <iostream>
#include <vector>

using namespace std;

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

	int N, K;
	cin >> N >> K;
	
	vector<int> p;
	for (int i = 1; i <= N; i++)
	{
		p.push_back(i);
	}

	cout << "<";
	int prevIndex = 0;
	while (true)
	{
		int size = p.size();
		if (p.size() == 1)
		{
			cout << p[0] << ">";
			break;
		}

		int index = prevIndex + K - 1;
		if (size <= index)
		{
			index %= size;
		}
		cout << p[index] << ", ";
		p.erase(p.begin() + index);
		prevIndex = index;
	}

	return 0;
}