这道题利用队列来模拟,用两个指针控制队列的头和尾,进队列和出对列用指针来操作。
#include <stdio.h>
#include <string.h>
int que[10010];
int main()
{
int n,i,s=0,e=0,m;
int num;
char st[10];
scanf("%d",&n);
while(n--)
scanf("%d",&que[e++]);
scanf("%d%*c",&m);
while(m--)
{
scanf("%s",st);
if(strcmp(st,"JOIN")==0)
{
scanf("%d",&num);
que[e++]=num; //插入到队尾;
}else if(strcmp(st,"ASK")==0)
{
scanf("%d",&num);
printf("%d\n",que[s+num-1]);
}else if(strcmp(st,"LEAVE")==0)
{
scanf("%d",&num);
for(i=s+num-1;i<e-1;i++)
que[i]=que[i+1]; //中间有出队情况,后面的元素前移;
e--;
}else if(strcmp(st,"FINISH")==0)
{
scanf("%d",&num); //队头有出队,头指针后移;
s+=num;
}else if(strcmp(st,"LENGTH")==0)
printf("%d\n",e-s); //尾指针减头指针为队列长度;
}
return 0;
}
#include <stdio.h>
#include <string.h>
int que[10010];
int main()
{
int n,i,s=0,e=0,m;
int num;
char st[10];
scanf("%d",&n);
while(n--)
scanf("%d",&que[e++]);
scanf("%d%*c",&m);
while(m--)
{
scanf("%s",st);
if(strcmp(st,"JOIN")==0)
{
scanf("%d",&num);
que[e++]=num; //插入到队尾;
}else if(strcmp(st,"ASK")==0)
{
scanf("%d",&num);
printf("%d\n",que[s+num-1]);
}else if(strcmp(st,"LEAVE")==0)
{
scanf("%d",&num);
for(i=s+num-1;i<e-1;i++)
que[i]=que[i+1]; //中间有出队情况,后面的元素前移;
e--;
}else if(strcmp(st,"FINISH")==0)
{
scanf("%d",&num); //队头有出队,头指针后移;
s+=num;
}else if(strcmp(st,"LENGTH")==0)
printf("%d\n",e-s); //尾指针减头指针为队列长度;
}
return 0;
}