C++)
#include <iostream>
#include <queue>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
queue<int> q;
for (int i = 1; i <= n; i++)
q.push(i);
bool ord = false;
while (q.size() != 1) {
if (ord)
q.push(q.front());
else
cout << q.front() << " ";
q.pop();
ord = !ord;
}
cout << q.front();
return 0;
}
Python)
import sys
from collections import deque
read = sys.stdin.readline
n = int(read().rstrip())
q = deque()
for i in range(n):
q.append(i+1)
ord = False
ans = []
while len(q) != 1:
if ord:
q.append(q[0])
else:
ans.append(q[0])
ord = not ord
q.popleft()
ans.append(q[0])
print(*ans)'백준 1 > 자료구조' 카테고리의 다른 글
| [백준 10866] 덱 (C++/Python) (0) | 2020.12.06 |
|---|---|
| [백준 1158] 요세푸스 문제 (C++/Python) (0) | 2020.12.06 |
| [백준 10845] 큐 (C++/Python) (0) | 2020.12.06 |
| [백준 9012] 괄호 (C++/Python) (0) | 2020.12.06 |
| [백준 10828] 스택 (C++/Python) (0) | 2020.12.06 |