Uva10935 - Throwing cards away I
2017-11-29 18:10
435 查看
一.题目
题目链接:Uva10935二.思路
典型的队列,对于每次的排,把第一个pop出来输出,第二个pop出来重新push进队尾,只剩1张牌的时候结束,注意只有一张牌时候的情况,详情看代码注释。三.源代码
#include <iostream>#include <queue>
using namespace std;
int main() {
queue<int> cards; //用队列做容器
int n, temp1, temp2;
while ((cin >> n) && (n != 0)) {
for (int i = 1; i <= n; i++)
cards.push(i);
if (n == 1) {
//如果n=1直接输出
cout << "Discarded cards:" << endl << "Remaining card: 1" << endl;
queue<int> empty;
swap(cards, empty);
continue;
}
cout << "Discarded cards: ";
while (n >= 2) {
temp1 = cards.front();//输出队头
cards.pop();
if (n == 2) //注意最后一个数据不需要逗号
cout <<temp1 << endl;
else
cout<<temp1 << ", ";
temp2 = cards.front();//第二个元素添加至队尾
cards.pop();
cards.push(temp2);
n--;
}
cout << "Remaining card: " << temp2 << endl;
queue<int> empty; //清空容器
swap(cards, empty);
}
return 0;
}
相关文章推荐
- Throwing cards away I UVA - 10935
- UVa10935-Throwing cards away I-卡片游戏-队列的运用
- UVA 10935 - Throwing cards away I
- UVa 10935 (水题) Throwing cards away I
- UVa10935 - Throwing cards away I
- UVa 10935 - Throwing cards away I
- Throwing cards away I 卡牌游戏 UVA 10935
- UVa 10935 - Throwing cards away I
- UVA -10935-卡片游戏-Throwing cards away
- Throwing cards away I UVA - 10935
- UVA 10935 - Throwing cards away I
- UVA 10935 Throwing cards away
- UVa 10935 Throwing cards away I【队列模拟】
- UVa 10935 - Throwing cards away I
- UVa 10935 Throwing cards away I(习题5-3)
- UVA10935 Throwing cards away I【模拟+queue+循环队列】
- UVA - 10935-Throwing cards away I
- 习题5-3 UVA 10935 Throwing cards away I 卡片游戏
- Uva 10935 Throwing cards away I
- 经典第五章 习题 5-3 UVA 10935 Throwing cards away I(队列的简单应用)