设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍+——+即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列请按业务完成的顺序输出顾客序列,用c语言编译程序
时间: 2024-06-12 19:10:46 浏览: 5
这是一个典型的银行排队问题,可以使用队列来解决。我们可以将顾客按照到达时间依次加入队列中,然后根据A、B窗口的处理速度,依次从队列中取出顾客进行处理。具体实现可以参考以下代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int id; // 顾客编号
int time; // 顾客需要处理的时间
} Customer;
typedef struct {
Customer data[MAX_SIZE];
int front, rear;
} Queue;
void initQueue(Queue *q) {
q->front = q->rear = 0;
}
int isEmpty(Queue *q) {
return q->front == q->rear;
}
int isFull(Queue *q) {
return (q->rear + 1) % MAX_SIZE == q->front;
}
void enqueue(Queue *q, Customer c) {
if (isFull(q)) {
printf("Queue is full!\n");
exit(1);
}
q->data[q->rear] = c;
q->rear = (q->rear + 1) % MAX_SIZE;
}
Customer dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty!\n");
exit(1);
}
Customer c = q->data[q->front];
q->front = (q->front + 1) % MAX_SIZE;
return c;
}
int main() {
Queue qA, qB;
initQueue(&qA);
initQueue(&qB);
int n, id, time;
printf("Enter the number of customers: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter the id and time of customer %d: ", i + 1);
scanf("%d%d", &id, &time);
Customer c = {id, time};
enqueue(&qA, c);
}
int curTime = 0;
while (!isEmpty(&qA) || !isEmpty(&qB)) {
if (!isEmpty(&qA)) {
Customer c = dequeue(&qA);
printf("Processing customer %d at A window\n", c.id);
curTime += c.time;
if (isEmpty(&qA) || curTime % 2 == 0) {
if (!isEmpty(&qB)) {
Customer c = dequeue(&qB);
printf("Processing customer %d at B window\n", c.id);
curTime += c.time;
}
} else {
enqueue(&qB, c);
}
} else {
Customer c = dequeue(&qB);
printf("Processing customer %d at B window\n", c.id);
curTime += c.time;
}
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)