【2019.4.7】勇士总冠军
需要注意的地方:有可能只有1张牌
本题的数据结构:
整数序列 + 队首删除操作 + 队尾插入操作 = 队列
#include <iostream>
#include <queue>
using namespace std;
queue<int> cards;
int main()
{
//freopen("C:\\Users\\Summer\\Desktop\\input.txt", "r", stdin);
//freopen("C:\\Users\\Summer\\Desktop\\output.txt", "w", stdout);
int n;
while(cin>>n && n) {
//清空队列
while(!cards.empty()) cards.pop();
//给队列赋初值
for(int i=1; i<=n; i++) cards.push(i);
//开始操作
cout<<"Discarded cards:";
int num = 0;
while((int)cards.size() >= 2) {
//如果不是第一次丢牌,要加逗号
if(num++) cout<<',';
//丢弃第一张牌
cout<<' '<<cards.front();
cards.pop();
//把第二张牌插到最后
cards.push(cards.front());
cards.pop();
}
cout<<endl;
cout<<"Remaining card: "<<cards.front()<<endl;
}
return 0;
}