7-1 银行业务队列简单模拟 (25分)
设某银行有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
Accepted Code
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <string>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Position;
typedef int ElementType;
struct QNode {
ElementType *Data; /* 存储元素的数组 */
Position Front, Rear; /* 队列的头、尾指针 */
int MaxSize; /* 队列最大容量 */
};
typedef struct QNode *Queue;
Queue CreateQueue(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->Front == Q->Rear);
}
ElementType DeleteQ(Queue Q) {
if (IsEmpty(Q)) {
//printf("队列空");
return ERROR;
} else {
Q->Front = (Q->Front + 1) % Q->MaxSize;
return Q->Data[Q->Front];
}
}
int main() {
int N;
scanf("%d", &N);
Queue Q1, Q2;
Q1 = CreateQueue(1005);
Q2 = CreateQueue(1005);
for (int i = 0; i < N; i++) {
int n;
scanf("%d", &n);
//奇数放入1队列,偶数放入2队列
if (n % 2) {
AddQ(Q1, n);
} else AddQ(Q2, n);
}
//printf("1\n");
int Arr[1005], len = 0;
while (!IsEmpty(Q1) || !IsEmpty(Q2)) {
if (!IsEmpty(Q1)) {
Arr[len++] = DeleteQ(Q1);
Arr[len++] = DeleteQ(Q1);
}
if (!IsEmpty(Q2)) {
Arr[len++] = DeleteQ(Q2);
}
}
for (int i = 0; i < len; ++i) {
printf("%d", Arr[i]);
if (i != len - 1) printf(" ");
}
return 0;
}
仅供参考