版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39651984/article/details/79038949
桌上有n(n<=50)张牌,从第一张牌(即顶面的牌)开始从上往下一次编号为1—n。当至少还剩两张牌时进行以下操作:把第一张牌扔掉,然后把新的第一张牌放到整叠牌的最后。输入每行包含一个n,输出扔掉牌,以及剩下的牌
【分析】因为对牌的操作是入队和出对,直接用STL里面的queue来模拟牌堆即可
如果你PE了,很有可能 当N=1时,直接输出“Discarded cards:”末尾不能有空格!
直接上代码:
#include <bits/stdc++.h>
using namespace std;
#define _rep(i,a,b) for(int i = (a); i <= (b); ++i)
int main(int argc, char const *argv[])
{
int n;
while(cin >> n && n){
queue<int> q;
_rep(i,1,n) q.push(i);
cout << "Discarded cards:";
bool first = true;
while(q.size() >= 2){
if(first){first = false; cout << " " << q.front();}
else cout << ", " << q.front();
q.pop();
q.push(q.front());
q.pop();
}
cout << endl << "Remaining card: " << q.front() << endl;
}
return 0;
}