1158번: 요세푸스 문제

Memo


큐를 사용해서 계속 빼고 넣고 하면 된다!!

K - 1만큼 빼고 1개는 pop!!

Code


제출 날짜

2021/04/23

메모리

2148 KB

시간

64 ms

#include <iostream>
#include <queue>

int N, K;
std::queue<int> yos;
std::vector<int> res;

void output()
{
	std::cout << "<";
	for(auto& i : res)
	{
		std::cout << i;
		if (i == res[N - 1])
			break;
		std::cout << ", ";
	}
	std::cout << ">";
}

void solution()
{
	int tmp;

	while(!yos.empty())
	{
		for(int i = 0 ; i < K - 1 ; ++i)
		{
			tmp = yos.front();
			yos.pop();
			yos.push(tmp);
		}
		res.push_back(yos.front());
		yos.pop();
	}
}

void input()
{
	std::cin >> N >> K;
	for (int i = 1 ; i <= N ; ++i)
		yos.push(i);
}

void preset()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	std::cout.tie(NULL);
}

int main()
{
	preset();
	input();
	solution();
	output();
}