백준 11866 요세푸스 문제 0

CJB_ny·2023년 1월 5일
0

백준

목록 보기
39/104
post-thumbnail

요세푸스 문제 0

이전과 비슷한 queue를 사용해서 풀었다.

그림판에다가 그려보면서 도식화를 하니까 돌고도는 느낌?

먼저 pop했다가 원하는 값이 아니라면은 다시 push해주고

이것을 queue가 빌때까지 반복을 하면됨.

#include <iostream>
#include <queue>
using namespace std;
#define endl "\n"

int n, k;
queue<int> q;

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

	cin >> n >> k;

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

	cout << "<";
	
	int pCnt = 0;
	while (!q.empty())
	{
		if (q.size() == 1)
		{
			cout << q.front(); 
			break;
		}

		int val = q.front();
		q.pop();
		++pCnt;

		if (pCnt == k)
		{
			pCnt = 0;
			cout << val << ", ";
		}
		else q.push(val);
	}

	cout << ">";

	return 0;
}
profile
https://cjbworld.tistory.com/ <- 이사중

0개의 댓글