7-18银行业务队列简单模拟

题目描述

题目地址为:https://pintia.cn/problem-sets/15/problems/825

设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出。

输入格式:

输入为一行正整数,其中第1个数字N(≤1000)为顾客总数,后面跟着N位顾客的编号。编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口。数字间以空格分隔。

输出格式:

按业务处理完成的顺序输出顾客的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。

输入样例:

1
8 2 1 3 9 4 11 13 15

输出样例:

1
1 3 2 9 11 4 13 15

思考

设置两个队列然后按照指定规则出队列

具体代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>

typedef struct {
int data[1001];
int front,rear;
} Queue;

int out[1000];

int main(){
Queue QA,QB;
int people;
scanf("%d",&people);
QA.front = QA.rear = 0;
QB.front = QB.rear = 0;
while(people){
int n;
scanf("%d",&n);
if(n % 2 != 0) {
QA.data[QA.rear++] = n;
} else {
QB.data[QB.rear++] = n;
}
people--;
}
int index = 0;
while(QA.rear > QA.front || QB.rear > QB.front) {
if (QA.rear > QA.front) {
out[index++] = QA.data[QA.front++];
if (QA.rear > QA.front){
out[index++] = QA.data[QA.front++];
}
}

if (QB.rear > QB.front) {
out[index++] = QB.data[QB.front++];
}
}
int flag = 0;
for(int i = 0; i < index; i++) {
printf("%d",out[i]);
if(i != index - 1) {
printf(" ");
}
}

return 0;
}