银行业务队列简单模拟
设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出。
输入格式:
输入为一行正整数,其中第1个数字N(≤1000)为顾客总数,后面跟着N位顾客的编号。编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口。数字间以空格分隔。
输出格式
按业务处理完成的顺序输出顾客的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。
输入样式
8 2 1 3 9 4 11 13 15
输出样式
1 3 2 9 11 4 13 15
代码
#include <stdio.h>
#include <stdlib.h>
#define MAXQSIZE 1000
#define OK 0
#define ERROR -1
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
typedef struct{
QElemType *base;
int front;
int rear;
} SqQueue;
Status InitQueue_Sq(SqQueue &Q)
{
Q.base = (QElemType *) malloc(MAXQSIZE * sizeof(QElemType));
if (NULL == Q.base)
return OVERFLOW;
Q.front = 0;
Q.rear = 0;
return OK;
}
Status EnQueue_Sq(SqQueue &Q, QElemType e)
{
int k;
//1 如果空间已经满了,返回出错
if ((Q.rear + 1) % MAXQSIZE == Q.front)
return ERROR;
//2 把e放入rear的位置上
Q.base[Q.rear] = e;
//3 善后
Q.rear = (Q.rear + 1) % MAXQSIZE;
return OK;
}
int DeQueue_Sq(SqQueue &Q)
{
int k;
//1 如果队列已经空
if (Q.rear == Q.front)
return ERROR;
//2 记录front位置的元素到e
k = Q.base[Q.front];
//3 善后
Q.front = (Q.front + 1) % MAXQSIZE;
return k;
}
int QueueLength_Sq(SqQueue Q)
{
return ((Q.rear - Q.front + MAXQSIZE) % MAXQSIZE);
}
bool QueueEmpty_Sq(SqQueue Q)
{
return (Q.rear == Q.front);
}
int main()
{
SqQueue QA, QB;
int n,i,d;
InitQueue_Sq(QA);
InitQueue_Sq(QB);
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&d);
if(d % 2 == 0)
EnQueue_Sq(QB,d);
else
EnQueue_Sq(QA,d);
}
while(!QueueEmpty_Sq(QA) && !QueueEmpty_Sq(QB))
{
if(QueueLength_Sq(QA)==1 && QueueEmpty_Sq(QB))
printf("%d",DeQueue_Sq(QA));
else
printf("%d ",DeQueue_Sq(QA));
if(!QueueEmpty_Sq(QA))
{
if(QueueLength_Sq(QA)==1 && QueueEmpty_Sq(QB))
printf("%d",DeQueue_Sq(QA));
else
printf("%d ",DeQueue_Sq(QA));
}
if(QueueLength_Sq(QB)==1 && QueueEmpty_Sq(QA))
printf("%d",DeQueue_Sq(QB));
else
printf("%d ",DeQueue_Sq(QB));
}
if(QueueEmpty_Sq(QA) && !QueueEmpty_Sq(QB))
{
while(!QueueEmpty_Sq(QB))
if(QueueLength_Sq(QB)==1 && QueueEmpty_Sq(QA))
printf("%d",DeQueue_Sq(QB));
else
printf("%d ",DeQueue_Sq(QB));
}
if(!QueueEmpty_Sq(QA) && QueueEmpty_Sq(QB))
{
while(!QueueEmpty_Sq(QA))
if(QueueLength_Sq(QA)==1 && QueueEmpty_Sq(QB))
printf("%d",DeQueue_Sq(QA));
else
printf("%d ",DeQueue_Sq(QA));
}
return 0;
}