c语言之 练习题8 排队

本文是接着上一篇c语言之 练习题2 大贤者福尔:子串,巅峰日_水w的博客-CSDN博客

目录

排队

代码1:c++

代码2:pytohn


排队

排队是日常生活中经常遇到的事情,如超市结账,餐厅就餐等。有时候,因为队伍实在是太长,或者比较着急,排队过程中经常会遇到插队的情况。

允许插队时,一个人到达餐厅时会先看看队列里面是否有熟人,如果发现了熟人,他就会插队并排到熟人的后面。如果没有熟人在排队,他就只能在队尾排队。有时候,队伍中的某个或某些相熟的同学实在不想排队了,他们就会离队。队伍中的人只有在轮到自己时,才会办理业务并离队。

小A正在研究允许插队的排队问题,他准备编写一个计算机程序,模拟排队的过程。他把相熟的人定义为若干小组,每个小组中的人都是相熟的,相熟的人排队时会插队。在此基础上,他定义了一系列指令,表示排队过程中发生的情况。

  • enqueue x: x开始排队;
  • dequeue: 队头的人办理完业务离队;
  • deqteam x:x及相熟的人(若存在)离队;
  • stop:停止模拟;

输入

输入数据有若干组,每组为的第一行为一个整数gg,表示一共有多少个小组。随后的gg行中,每行表示一个小组。小组行的最前面为一个整数nn,表示小组中有nn个人,随后有空格分隔的nn个名单,每个名字最长不超过10个字符,由大小写字母或数字构成。随后的若干行为排队模拟指令,stop指令表示该组数据结束。

输出

对每组测试数据,在第一行输出一个Case #k:,k为测试数据的组号,从1开始。对于每个dequeue指令,若队列非空,则在单独的行中输出出队者的名字;对于每个deqteam指令,在单独的行中输出所有出队者的名字,以空格分隔。若队列为空,则不输出任何内容。

示例输入

2
3 101 102 103
3 201 202 203
enqueue 101
enqueue 201
enqueue 102
enqueue 202
deqteam 102
enqueue 203
dequeue
dequeue
dequeue
dequeue
dequeue
dequeue
stop
2
5 259001 259002 259003 259004 259005
6 260001 260002 260003 260004 260005 260006
enqueue 259001
enqueue 260001
enqueue 259002
enqueue 259003
enqueue 259004
enqueue 259005
dequeue
dequeue
enqueue 260002
enqueue 260003
deqteam 260002
dequeue
dequeue
dequeue
stop
0

示例输出

Case #1:
101 102
201
202
203
Case #2:
259001
259002
260001 260002 260003
259003
259004
259005

代码1:c++

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(int n)
{
    map<string, int> zuhao;
    list<int> group;
    vector<queue<string>> run(n + 1);
    for(int i = 1; i <= n; i++)
    {
        int num;
        cin >> num;
        for (int j = 1; j <= num; j++)
        {
            string member;
            cin >> member;
            zuhao.insert(make_pair(member, i));
        }
    }
    string q;
    while (cin >> q && q != "stop")
    {
        if (q == "enqueue")
        {
            string tmp;
            cin >> tmp;
            int index;
            if (zuhao.count(tmp))
            {
                index = zuhao[tmp];
            }
            else
            {
                index = run.size();
                zuhao.insert(make_pair(tmp, index));
                run.resize(index + 1);
            }
            if (run[index].empty())
            {
                group.push_back(index);
            }
            run[index].push(tmp);
        }
        else if (q == "deqteam")
        {
            string tmp;
            cin >> tmp;
            int index = zuhao[tmp];
            while (!run[index].empty())
            {
                cout << run[index].front();
                run[index].pop();
                if (!run[index].empty())
                    cout << " ";
                else
                    cout << endl;
            }
            for (auto it = group.begin(); it != group.end(); it++)
            {
                if (*it == index)
                {
                    it = group.erase(it);
                    break;
                }
            }
        }
        else if (q == "dequeue")
        {
            if (group.empty())
                continue;
            int first = group.front();
            cout << run[first].front() << endl;
            run[first].pop();
            if (run[first].empty())
            {
                group.pop_front();
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, cnt = 0;
    while (cin >> n && n)
    {
        cnt++;
        cout << "Case #" << cnt << ":\n";
        solve(n);
    }
    return 0;
}

代码2:pytohn

k = 1
while 1:
    g = int(input())
    if g == 0: break
    in_q = [0 for _ in range(10010)]  # 记录队伍是否已入队
    dict = []  # 记录各队人员
    dict_in_q = [[] for _ in range(10010)]  # 记录各队已入队人员
    q_id = {}  # 记录每个人所在队伍编号
    q = []  # 排队记录 记录队伍号
    q_id_i = 0  # 队伍号计数器
    for i in range(g):
        acqts = input().split()  # 熟人组
        n = acqts[0]
        dict.append(acqts[1:])
        q_id_i += 1
        for id in acqts[1:]:  # 给队伍编号并记录队伍详情
            # if id in dict.keys(): # 是否会出现一个人在多个熟人组
            q_id[id] = i  # 记录每个人所在队伍号
    print("Case #%d:" % k)
    queue = []
    k += 1
    while 1:
        op_str = input().split()
        op = op_str[0]
        if op == "stop": break
        if op == "dequeue":  # 队头出队
            if q:
                qid = q[0]
                dq_id = dict_in_q[qid].pop(0)
                print(dq_id)
                if not dict_in_q[qid]:  # 队员已全部出队
                    in_q[qid] = 0  # 队伍出队
                    del q[0]
            continue
        id = op_str[1]
        if id not in q_id.keys():  # 没有队伍,初始化队伍信息
            q_id[id] = q_id_i
            dict.append([id])  # 创建队伍到队伍字典
            q_id_i += 1
        qid = q_id[id]
        if op == "enqueue":  # 入队
            if in_q[qid] == 0:  # 还未入队
                in_q[qid] = 1
                q.append(qid)
                dict_in_q[qid] = [id]
            else:
                dict_in_q[qid].append(id)
        elif op == "deqteam":  # x及相熟的人(若存在)离队
            in_q[qid] = 0  # 队伍出队
            print(" ".join(str(x) for x in dict_in_q[qid]))
            dict_in_q[qid].clear()  # 清空队伍入队人员记录
            q.remove(qid)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水w

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值