UVA 540 Team Queue【queue的用法】

source:

点击打开链接


题意:就是模拟平时食堂排队,学生分为不同的组,然后每来一个学生先从前往后看有没有自己一个组的,有的话就插列插到自个儿组最后一个。现在给出分组名单然后进行多次询问,输出每次出队询问时出队的学生编号。

思路:对于每个组分别用一个queue模拟,然后组号也进行queue模拟即可


关于queue的用法:

头文件:#include<queue>

声明:queue<int>  q

基本操作:

  • q.push(x) 将x加到队尾
  • q.pop()  将队头第一个元素弹出 注意:并不返回该元素的值
  • q.front()  返回队头元素值
  • q.back()  返回对尾元素值
  • q.empty(),当队列空时,返回true
  • q.size() 返回队列中元素的个数
注意:队列没有clear()方法!


代码如下:

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;

int belong[1000005],isinq[1005];
queue<int> seq;
queue<int> q[1005];

int main()
{
    int t,n,team,x,T=1;
    char s[10];
    while(scanf("%d",&t)!=EOF && t!=0)
    {
        printf("Scenario #%d\n",T++);
        memset(belong,0,sizeof(belong));
        memset(isinq,0,sizeof(isinq));
        for(int i=1;i<=t;i++) while(!q[i].empty()) q[i].pop();
        while(!seq.empty()) seq.pop();
        for(int i=1;i<=t;i++)
        {
            scanf("%d",&n);
            for(int j=0;j<n;j++)
            {
                scanf("%d",&x);
                belong[x]=i;
            }
        }

        while(scanf("%s",s)!=EOF && s[0]!='S')
        {
            if(s[0]=='E')
            {
                scanf("%d",&x);
                team=belong[x];
                if(isinq[team]==0)
                {
                    seq.push(team);
                    isinq[team]=1;
                }
                q[team].push(x);
            }
            else
            {
                team=seq.front();
                printf("%d\n",q[team].front());
                q[team].pop();
                if(q[team].empty())
                {
                    isinq[team]=0;
                    seq.pop();
                }
            }
        }
        printf("\n");
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值