用c++设计代码完成任务:假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙。当有窗口空闲时,下一位顾客即去该窗口处理事务。当有多个窗口可选择时,假设顾客总是选择编号最小的窗口。 有些银行会给VIP客户以各种优惠服务,例如专门开辟VIP窗口。为了最大限度地利用资源,VIP窗口的服务机制定义为:当队列中没有VIP客户时,该窗口为普通顾客服务;当该窗口空闲并且队列中有VIP客户在等待时,排在最前面的VIP客户享受该窗口的服务。同时,当轮到某VIP客户出列时,若VIP窗口非空,该客户可以选择空闲的普通窗口;否则一定选择VIP窗口。
时间: 2023-05-29 17:03:41 浏览: 47
以下是用C语言设计的代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX_CUSTOMER 1000 // 最大顾客数量
#define MAX_WINDOW 10 // 最大窗口数量
#define VIP_WINDOW 3 // VIP窗口编号
typedef struct {
int arrival_time; // 到达时间
int service_time; // 服务时间
int window_id; // 窗口编号
int is_vip; // 是否为VIP客户
} Customer;
Customer customers[MAX_CUSTOMER]; // 顾客队列
int windows[MAX_WINDOW]; // 窗口数组
int cmp(const void *a, const void *b) {
Customer *c1 = (Customer *)a;
Customer *c2 = (Customer *)b;
if (c1->arrival_time != c2->arrival_time) {
return c1->arrival_time - c2->arrival_time;
} else {
return c1->is_vip - c2->is_vip;
}
}
int find_next_customer(int n) {
int i;
int next_customer = -1;
for (i = 0; i < n; i++) {
if (customers[i].window_id == -1 && (next_customer == -1 || customers[i].arrival_time < customers[next_customer].arrival_time)) {
next_customer = i;
}
}
return next_customer;
}
int find_vip_customer(int n) {
int i;
int vip_customer = -1;
for (i = 0; i < n; i++) {
if (customers[i].is_vip && customers[i].window_id == -1 && (vip_customer == -1 || customers[i].arrival_time < customers[vip_customer].arrival_time)) {
vip_customer = i;
}
}
return vip_customer;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
int i;
for (i = 0; i < n; i++) {
int hh, mm, ss, service_time, is_vip;
scanf("%d:%d:%d%d%d", &hh, &mm, &ss, &service_time, &is_vip);
customers[i].arrival_time = hh * 3600 + mm * 60 + ss;
customers[i].service_time = service_time * 60;
customers[i].window_id = -1;
customers[i].is_vip = is_vip;
}
qsort(customers, n, sizeof(Customer), cmp);
int j;
for (i = 0; i < k; i++) {
windows[i] = 8 * 3600; // 窗口初始时间为8点
}
int served_count = 0;
int vip_count = 0;
while (served_count < n) {
int next_customer = find_next_customer(n);
if (next_customer == -1) { // 没有顾客排队
break;
}
int current_time = customers[next_customer].arrival_time;
if (current_time >= 21 * 3600) { // 超过21点
break;
}
if (current_time < 8 * 3600) { // 还没到8点
current_time = 8 * 3600;
}
int vip_customer = find_vip_customer(n);
if (vip_customer != -1 && customers[vip_customer].arrival_time <= current_time && (vip_count < k / 2 || windows[VIP_WINDOW] <= current_time)) {
// 有VIP客户可以服务
if (customers[vip_customer].arrival_time < current_time) {
customers[vip_customer].wait_time += current_time - customers[vip_customer].arrival_time;
}
customers[vip_customer].window_id = VIP_WINDOW;
windows[VIP_WINDOW] = current_time + customers[vip_customer].service_time;
vip_count++;
served_count++;
} else {
// 没有VIP客户可以服务
int min_window = 0;
for (j = 1; j < k; j++) {
if (windows[j] < windows[min_window]) {
min_window = j;
}
}
if (windows[min_window] <= current_time) {
// 窗口空闲
customers[next_customer].window_id = min_window;
windows[min_window] = current_time + customers[next_customer].service_time;
} else {
// 窗口忙碌
customers[next_customer].wait_time = windows[min_window] - current_time;
customers[next_customer].window_id = min_window;
windows[min_window] += customers[next_customer].service_time;
}
served_count++;
}
}
int total_wait_time = 0;
int total_count = 0;
for (i = 0; i < n; i++) {
if (customers[i].window_id != -1) {
total_wait_time += customers[i].wait_time;
total_count++;
}
}
printf("%.1f", (double)total_wait_time / total_count / 60.0);
return 0;
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)