标签:c++ 思路分析 discard return car its lang 输出 一个
桌上有一叠牌,自上而下编号为1~n。若桌上牌数大于1张,那么丢弃一张顶部牌后,再将现在的顶部牌移到最后。要求给出模拟过程和最终剩余的牌号
典型队列模拟,丢弃即出队,移到最后即入队,但队列大小为1时停止操作
Discarded cards:
后无需空格#include<bits/stdc++.h>
using namespace std;
int n;
int main() {
while (cin >>n && n != 0) {
printf("Discarded cards:");
queue<int> q;
for (int i = 1; i <= n; i ++) q.push(i);
while (q.size() != 1) {
printf("%s%d", q.front() == 1 ? " " : ", ", q.front()); // 第一个退出无输出,
q.pop();
q.push(q.front()); q.pop(); // 头入队后删除
}
printf("\nRemaining card: %d\n", q.front());
}
return 0;
}
UVA - 10935:Throwing cards away I (简单模拟)
标签:c++ 思路分析 discard return car its lang 输出 一个
原文地址:https://www.cnblogs.com/RioTian/p/13027955.html