问题:
设某银行有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 true 1
#define false -1
#define ERROR 0
typedef int ElementType;
typedef struct QNode * Queue;
typedef int Position;
struct QNode{
ElementType *Data;
Position Front,Rear;
int MaxSize;
};
Queue MakeQueue(int MaxSize){
Queue Q=(Queue)malloc(sizeof(struct QNode));
Q->Data=(ElementType*)malloc(MaxSize*sizeof(ElementType));
Q->Front=Q->Rear=0;
Q->MaxSize=MaxSize;
return Q;
}
bool IsFull(Queue Q){
return ((Q->Rear +1)%Q->MaxSize==Q->Front);
}
bool AddQ(Queue Q,ElementType X){
if(IsFull(Q)){
printf("队列满");
return false;
}
else{
Q->Rear =(Q->Rear+1)%Q->MaxSize;
Q->Data[Q->Rear]=X;
return true;
}
}
bool IsEmpty(Queue Q){
return (Q->Rear ==Q->Front );
}
ElementType Delete(Queue Q){
if(IsEmpty(Q)){
printf("队列空");
return ERROR;
}
else{
Q->Front=(Q->Front +1)%Q->MaxSize;
return Q->Data[Q->Front];
}
}
void Destory(Queue Q){
free(Q->Data );
Q->Data=NULL;
Q->Front=Q->Rear=0;
}
void Input(Queue A,Queue B){//把元素输进队列
int i,n,index;//index用来存元素
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&index);
if(index%2==0){
AddQ(B,index);
}else{
AddQ(A,index);
}
}
}
void Deal(Queue A,Queue B){//处理数据并输出
int flag=0;
while(!IsEmpty(A)&&!IsEmpty(B)){
if(flag==0){
printf("%d",Delete(A));
flag=1;
}else{
printf(" %d",Delete(A));
}
if(!IsEmpty(A)){
printf(" %d",Delete(A));
}
printf(" %d",Delete(B));
}
while(!IsEmpty(A)){
if(flag==0){
printf("%d",Delete(A));
flag=1;
}else{
printf(" %d",Delete(A));
}
}
while(!IsEmpty(B)){
if(flag==0){
printf("%d",Delete(B));
flag=1;
}else{
printf(" %d",Delete(B));
}
}
}
int main(){
Queue A=MakeQueue(1000);
Queue B=MakeQueue(1000);
Input(A,B);
Deal(A,B);
Destory(A);
Destory(B);
return 0;
}
如有错误,欢迎指出
谢谢