一,问题描述
二,问题分析
整个问题就是模拟的一个过程,难点在于对命令的处理
1.由于要清空整个队列,采用deque.clear()函数
2.命令的输入不同,采取不同的对策
三,问题解答
#include<iostream>
#include<string>
#include<deque>
using namespace std;
int main() {
int n, m; //n:队列数 m:指令数
deque<int> list[10001];
while (cin >> n >> m) {
for (int i = 0; i < m; i++) {
string order;
cin >> order;
if (order == "INIT") { //如果输入的命令是INIT
for (int j = 1; j <= n; j++) {
list[j].clear(); //清空队列
}
}
if (order == "PUSH") { //如果输入的命令是PUSH
int listid;
int value;
cin >> listid >> value; //依次输入队列号,和值
list[listid].push_back(value);
}
if (order == "POP") { //如果输入的命令是POP
int listid; //输入要处理的队列号
cin >> listid;
if (list[listid].empty() == 1) { //如果该队列为空
cout<<"NULL"<<endl;
}
else {
cout << list[listid].front() << endl;//队列不为空的情况
list[listid].pop_front();
}
}
}
}
return 0;
}