题目描述补充说明:这里的排队和传统的排队有出入。正在游玩的人为队列的前两位所以正在游玩视为正在排队。机厅里有一台游戏机每次可供最多两人同时游玩。但是来玩的人显然不止两个!这个时候他们就需要排队了而你需要写一个程序维护这个队列并在他人游玩结束后通知接下来上场的人。在整个过程中有以下几种事件可能发生:start:一局游戏开始。若这不是第一局游戏则上一局的参与者按照原本的顺序回到队尾。此时你应该按在队列
#include <iostream>
#include <queue>
#include <unordered_set>
using namespace std;
int main() {
int n;
cin >> n;
queue<string> playingQueue; // 正在游玩的队列
queue<string> waitingQueue; // 排队的队列
unordered_set<string> playingSet; // 记录正在游玩的人,用于判断是否在排队
for (int i = 0; i < n; i++) {
string event;
cin >> event;
if (event == "start") {
if (playingQueue.empty()) {
cout << "Error" << endl;
} else {
while (!playingQueue.empty()) {
cout << playingQueue.front() << " ";
playingSet.erase(playingQueue.front());
playingQueue.pop();
}
cout << endl;
}
} else if (event == "arrive") {
string name;
cin >> name;
if (playingSet.count(name) == 1 || playingQueue.size() >= 2) {
cout << "Error" << endl;
} else {
waitingQueue.push(name);
playingSet.insert(name);
cout << "OK" << endl;
}
} else if (event == "leave") {
string name;
cin >> name;
if (playingSet.count(name) == 0) {
cout << "Error" << endl;
} else {
playingSet.erase(name);
cout << "OK" << endl;
}
}
}
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/iBy7 著作权归作者所有。请勿转载和采集!