结训赛第一次复习:栈和队列(二)队列的应用

衔接上一篇:

http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2709/pid/2135

这也是一道很典型地队列问题:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int m;
    int a[10010],n,i,b;
    char s[10];
    scanf("%d",&m);
    int top=m;
    int bot=1;
    for(i=1;i<=m;i++)
    {
        scanf("%d",&a[i]);
    }
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",s);
        if(strcmp(s,"JOIN")==0)
        {
            scanf("%d",&b);
            a[++top]=b;
        }
        else if(strcmp(s,"ASK")==0)
        {
            scanf("%d",&b);
            printf("%d\n",a[b+bot-1]);
        }
        else if(strcmp(s,"LENGTH")==0)
        {
            printf("%d\n",top-bot+1);
        }
        else if(strcmp(s,"FINISH")==0)
        {
            scanf("%d",&b);
            bot=bot+b;
        }
        else if(strcmp(s,"LEAVE")==0)
        {
            scanf("%d",&b);
            for(i=bot+b-1;i<=top;i++)
            {
                a[i]=a[i+1];
            }
            top--;
        }
    }
    return 0;
}
这道题很贴心的列出来了常见的集中操作,不过要注意的是,因为队列有了底,所以要在跟“长度相关”的时候要加上bot-1才是本体

猜你喜欢

转载自blog.csdn.net/weixin_44067773/article/details/87861779
今日推荐