개발일지/알고리즘

백준 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;
}

'개발일지 > 알고리즘' 카테고리의 다른 글

백준 5430번 : AC  (0) 2025.01.28
백준 1966번 : 프린터 큐  (0) 2025.01.28
백준 1874번 : 스택 수열  (0) 2025.01.25
백준 9012번 : 괄호  (0) 2025.01.24
백준 10867번 : 중복 빼고 정렬하기  (0) 2025.01.23