【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e155
Virtual Judge: https://vjudge.net/problem/UVA-10935
【解題想法】STL, Queue

#include <iostream>
#include <queue>
using namespace std;
int main() {
int n;
while (cin >> n){
if (n == 0) break;
queue <int> q;
for (int i = 0; i < n; i++){
q.push(i+1);
}
cout << "Discarded cards:";
for (int i = 0; i < n-1; i++){
if (i != 0) cout << ',';
cout << ' ' << q.front();
q.pop();
q.push(q.front());
q.pop();
}
cout << endl << "Remaining card: " << q.front() << endl;
}
}
Python 程式碼 (2019-08-06)
while True:
n = int(input())
if n == 0:
break
lst = []
discard = []
for i in range(1, n+1):
lst.append(i)
for i in range(n-1):
discard.append(lst.pop(0))
lst.append(lst.pop(0))
print('Discarded cards:', ', '.join([str(c) for c in discard]))
print('Remaining card:', lst[0])