单纯用队列不好模拟中间离队的人,因为队列只能取队首元素
所以用一维数组模拟队列
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
char s[10];
int a[10050];
int i,j,k,m,n,begin,end,x;
scanf("%d",&m);
for(i=0;i<=m-1;i++)
{
scanf("%d",&a[i]);
}
begin=0;end=m; //begin记录队首的下标,end记录队尾往后一个的下标
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
if(strcmp(s,"LENGTH")!=0)
scanf("%d",&x);
if(strcmp(s,"JOIN")==0)
{
a[end++]=x; //有人入队,end后移
}
if(strcmp(s,"FINISH")==0)
{
while(x--)
begin++; //有人买完饭,begin后移
}
if(strcmp(s,"ASK")==0)
{
printf("%d\n",a[begin+x-1]);
}
if(strcmp(s,"LENGTH")==0)
{
printf("%d\n",end-begin);
}
if(strcmp(s,"LEAVE")==0)
{
for(i=begin+x;i<=end-1;i++)
a[i-1]=a[i]; //中间的人离队,从后往前覆盖
end--; //end前移
}
}
return 0;
}