简单模拟单队列排序
【输入格式】
第一行为一个不大于20的正整数N,表示银行等候区能容纳的人数
接下来用若干行依时间顺序先后发生的"顾客想进银行"或"叫好"事件,格式分别是:
1. 顾客想进银行,用In表示,其中是顾客编号,为不大于1000000的正整数;
2. 叫号,用Calling表示。
最后一行是一个#符号表示输入结束。
【输出格式】
顾客想进银行,若顾客进入,则输出 joined. Total: 其中是该顾客的编号,是顾客进入后,等候区的人数
顾客想进银行,若因等候区满而被拒绝,则输出 rejected. 其中是该顾客的编号
叫号,若有顾客前往柜台,则输出 called. Total: 其中是该顾客的编号,是顾客去柜台后,等候区的人数
叫号,等候区无人,则输出 No one!
题目中的数据规模是100000个正整数,用该题目很明是用队列,但是原有的#include里面的 pop(),和push()函数会根据数据规模越来越耗时,所以呢根据题目的要求,我们只需要 pop()和push()函数,并且无需考虑队列中是否有元素和满元素的问题,因为有一个全局变量tot约束着他们。
代码如下。
#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<iostream>
#define N 100000+10
typedef struct Queue* Q;
struct Queue{
int data[N];
int front;
int real;
};
Q Init()
{
Q q;
q=(Q)malloc(sizeof(Queue));
q->front=q->real=0;
return q;
}
void push(Q q,int t)
{
q->data[q->front++]=t;
}
int pop(Q q)
{
return q->data[q->real++];
}
using namespace std;
int tot=0;
int main()
{
int x;
string ss;
Q q;
q=Init();
scanf("%d",&x);
getchar();
while(1)
{
cin>>ss;
//cout<<"sdh";
getchar();//此处的getchar()用来读取cin未能处理掉的回车符
if(ss=="#")
return 0;
if(ss=="In")
{
int n;
scanf("%d",&n);
getchar();//同上
if(tot<x)
{
tot++;
printf("%d joined. Total:%d\n",n,tot);
push(q,n);
}
else
{
printf("%d rejected.\n",n);
}
}
if(ss=="Calling")
{
//getchar();
if(tot<=x&&tot>0)
{
tot--;
printf("%d called. Total:%d\n",pop(q),tot);
}
else if(tot==0)
printf("No one!\n");
}
}
}