끄적끄적 코딩
article thumbnail

큐를 통해서 풀었습니다.

모든 값들을 큐에 차례대로 넣고 K-1만큼 큐에서 빼서 뒤로 보내고
K번째는 출력하고 pop해줍니다.

 

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

int main(int argc, char *argv[])
{
	int n, t;
	int temp;
	queue<int> q;

	cin >> n >> t;

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

	cout << "<";
	while (1) {
		for (int i = 0; i < t - 1; ++i) {
			temp = q.front();
			q.pop();
			q.push(temp);
		}
		cout << q.front();
		q.pop();
		if (!q.size()) {
			break;
		}
		cout << ", ";
	}	
	cout << ">";

	return 0;
}

검색 태그