问题描述
题目描述
假设有一家奶茶店,现在有一些人在排队等待取奶茶,同时也有人在取奶茶。 请你设计一个程序模拟这种情况下的奶茶队列管理。
假设每个人取奶茶的时间非常短,可以忽略不计,只需要考虑队列中的操作。
队列操作说明:
1. 当操作为 1 时,表示有人已经取走奶茶,从队列中删除该人的信息。
2. 当操作为 2 时,表示有新人加入排队,将该人的信息加入队列。
在一系列操作之后,你需要回答:下一个取奶茶的人是谁?
输入描述
第一行有一个整数 n,代表初始队列有 n 个人。
第二行有 n 个字符串,代表当前奶茶队列中的人。
第三行为一个整数 m,代表接下来将会有 m 次操作。
接下来一共有 m 行,代表共有 m 次操作。
如果是操作 1,那么该行只会有一个数字,代表有人取走了奶茶。
如果是操作 2,那么该行有一个数字和一个字符串,第一个数字 2 表示有人加入了奶茶队列,第二个字符串代表新加入的奶茶队列的人。
输出描述
输出只有一行,为下一个取奶茶的人。 如果已经没有去奶茶的人了,输出“There are no more people in the queue.”。
输入示例
5
Giselle Winter Aubree Wrenley Royalty
3
1
1
2 Andrew
输出示例
Aubree
python解法
# 导入queue队列模块
import queue
# 初始化队列
que = queue.Queue()
# 读取初始化队列长度
n = int(input())
# 读取排队名称
names = input().split()
# 将各名称放入队列中
for name in names:
que.put(name)
# 读取操作次数
m = int(input())
# 循环处理每次操作
for _ in range(m):
operation = input().split()
opt = int(operation[0])
# 操作1 : 出队
if opt == 1:
if not que.empty():
que.get()
# 操作2 : 入队
elif opt == 2:
que.put(operation[1])
# 判断队列是否为空
print("There are no more people in the queue.") if que.empty() else print(que.get())
C++解法
#include <iostream>
#include <queue>
#include <string>
int main(){
int n, m, opt; // n代表队列长度,m代表操作次数,opt代表输入指令
std::cin >> n;
std::string name;
std::queue<std::string> que; // 新建一个队列
// 入队
while (n--){
std::cin >> name;
que.push(name);
}
std::cin >> m;
while (m--){
std::cin >> opt;
// 操作1 : 出队
if (opt == 1 && !que.empty())
que.pop();
// 操作2 : 入队
else if (opt == 2){
std::cin >> name;
que.push(name);
}
}
// 判断队列是否为空
if (!que.empty())
std::cout << que.front() << std::endl;
else
std::cout << "There are no more people in the queue." << std::endl;
}