习题5_3 卡片游戏(Throwing cards away I, UVa 10935)
题目描述:
桌上有n(n <= 50)张牌,从第一张牌(即位于顶面的牌)开始,从上往下依次编号为1~n。当至少还剩下两张牌时进行以下操作:把第一张牌扔掉,然后把新的第一张牌放到整叠牌的最后。输入每行包含一个n,输出每次扔掉的牌以及最后剩下的牌。
#include<iostream>
#include<queue>
using namespace std;
int main() {
int n;
while(cin >> n) {
if(n == 0) break;
cout << "Discarded cards:";
queue<int> q;
for(int i = 1; i <= n; i++) q.push(i);
int cnt = 0;
while(q.size() > 1) {
if(++cnt > 1) cout << ", ";
else cout << " ";
cout << q.front(); q.pop();
q.push(q.front()); q.pop();
}
cout << "\nRemaining card: " << q.front() << "\n";
}
return 0;
}