【5-3卡片游戏】
使用队列明显好于数组,O(n)–>O(1)
代码:
#include<iostream>
#include<string>
#include<queue>
using namespace std;
const int maxn = 10000 + 10;
int main()
{
int n;
while (cin >> n && n)
{
queue<int>qq;
for (int i = 1;i <= n;i++)
qq.push(i);
cout << "Discarded cards:";
int flag = 0;
while (n--)
{
if (qq.size() == 1)
{
cout << "\nRemaining card: " << qq.front() << endl;break;
}
if (flag++) cout << ',';
cout <<' '<< qq.front();
qq.pop();
if (!qq.empty()) {
if (qq.size() == 1) {
cout << "\nRemaining card: " << qq.front() << endl;break;
}
qq.push(qq.front());
qq.pop();
}
}
}
return 0;
}