卡玛网编程基础课 | 排队取奶茶

问题描述

题目描述

假设有一家奶茶店,现在有一些人在排队等待取奶茶,同时也有人在取奶茶。 请你设计一个程序模拟这种情况下的奶茶队列管理。

假设每个人取奶茶的时间非常短,可以忽略不计,只需要考虑队列中的操作。 

队列操作说明: 

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值