문제 : https://www.acmicpc.net/problem/2161


[알고리즘풀이]

카드를 넣을 때는 뒤로 넣고, 뺄 때는 앞에서부터 뺴므로, 자료구조 큐(queue)를 이용하면 된다.

#include<iostream>
#include<queue>

using namespace std;

int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n; queue<int> q;
	cin >> n;
	for (int i = 1; i <= n; i++)
		q.push(i);
	while (!(q.size() == 1)) {
		cout << q.front() << ' ';
		q.pop();
		q.push(q.front());
		q.pop();
	}
	cout << q.front();
}

 

+ Recent posts