您的位置:首页 > 产品设计 > UI/UE

540 - Team Queue

2016-05-06 19:21 477 查看

Team Queue

PS:因为该题排版较麻烦,这里给出OJ网址:UVa540 - Team Queue

有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一个队友的身后。如果没有任何一个队友排队,则他会排到长队的队尾。输入每个团队中所有队员的编号,要求支持如下3种指令(前两种指令可以穿插进行)。

ENQUEUEx:编号为x的人进入长队。

DEQUEUE:长队的队首出队。

STOP:停止模拟。

对于每个DEQUEUE指令,输出出队的人的编号。

#include <iostream>
#include <queue>
#include <vector>
#include <string>
using namespace std;
const int maxNum = 1005;
// 存放team,一维下标表示组号
vector<int> team[maxNum];

// 获取n所在的team组号,t表示一共多少组
int getTeam(int n, int t) {
for(int i = 0; i < t; i++) {
for(int j = 0; j < team[i].size(); j++) {
if(team[i][j] == n) {
return i;
}
}
}
// 表示未找到
return -1;
}

int main() {
int kase = 0;
// t组队伍
int t;
while(cin >> t && t) {
cout << "Scenario #" << ++kase << endl;
// 编号处理完毕
for(int i = 0; i < t; i++) {
// 每组队伍有多少编号
int n;
cin >> n;
for(int j = 0; j < n; j++) {
// 编号
int num;
cin >> num;
team[i].push_back(num);
}
}
// 操作
string op;
// 多少个team,多少个队列
queue<int> teamQueue[maxNum];
// 总队列
queue<int> teams;
while(cin >> op) {
// STOP 退出
if(op[0] == 'S') {
break;
} else if(op[0] == 'E') {
// ENQUEUE 进队
// 进队编号
int num;
cin >> num;
// 获得num所在组号
int n = getTeam(num, t);
// 该编号未在team容器中
if(n == -1) {
continue;
}
// 如果第n个队列为空,将第n个队列加到总队列中
if(teamQueue
.empty()) {
teams.push(n);
}
// 将该编号加入第n个队列
teamQueue
.push(num);
} else if(op[0] == 'D') {
// DEQUEUE 出队
// 找到第一个不为空的队列
int n = teams.front();
cout << teamQueue
.front() << endl;
teamQueue
.pop();
// 队列n全体出列
if(teamQueue
.empty()) {
teams.pop();
}
}
}
cout << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UVa540 uva ACM