1. 模拟一个双服务员的排队系统; 这里,顾客排成一队,排在最前面的顾客到最先空出来的服务员处接受服务。顾客到来间隔时间服从参数为0.1的指数分布,对顾客的服务时间服从[4,15]上的均匀分布,排队按先到先服务规则,队长无限制。假设时间以min为单位,一个工作日为8h。 模拟一个工作日内完成服务的个数及顾客平均等待时间t。用C语言实现
时间: 2024-02-22 20:58:39 浏览: 19
好的,以下是一个简单的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_QUEUE_SIZE 1000
typedef struct {
int arrival_time; // 到达时间
int service_time; // 服务时间
} Customer;
typedef struct {
Customer queue[MAX_QUEUE_SIZE]; // 队列
int front; // 队首
int rear; // 队尾
} Queue;
int main() {
srand(time(NULL)); // 初始化随机数种子
Queue queue1 = { .front = 0, .rear = 0 }; // 初始化队列1
Queue queue2 = { .front = 0, .rear = 0 }; // 初始化队列2
int service_time1 = 0; // 服务时间1
int service_time2 = 0; // 服务时间2
int customer_count = 0; // 完成服务的顾客数量
int waiting_time_sum = 0; // 总等待时间
int total_time = 0; // 总时间
int i;
for (i = 0; i < 480; i++) { // 8小时,每分钟模拟一次
if (rand() % 10 == 0) { // 顾客到来
Customer customer = { .arrival_time = i };
customer.service_time = rand() % 12 + 4; // 服务时间为4-15分钟之间的随机数
if (queue1.rear - queue1.front <= queue2.rear - queue2.front) { // 排队选择短的队列
queue1.queue[queue1.rear++] = customer;
} else {
queue2.queue[queue2.rear++] = customer;
}
}
if (service_time1 > 0) { // 服务员1正在服务
service_time1--;
if (service_time1 == 0) { // 完成服务
customer_count++;
}
} else if (queue1.rear - queue1.front > 0) { // 服务员1没有服务,从队列1中取出下一个顾客进行服务
Customer customer = queue1.queue[queue1.front++];
waiting_time_sum += i - customer.arrival_time;
service_time1 = customer.service_time;
} else if (service_time2 > 0) { // 服务员2正在服务
service_time2--;
if (service_time2 == 0) { // 完成服务
customer_count++;
}
} else if (queue2.rear - queue2.front > 0) { // 服务员2没有服务,从队列2中取出下一个顾客进行服务
Customer customer = queue2.queue[queue2.front++];
waiting_time_sum += i - customer.arrival_time;
service_time2 = customer.service_time;
}
}
printf("完成服务的顾客数量:%d\n", customer_count);
printf("顾客平均等待时间:%.2f min\n", (float)waiting_time_sum / customer_count);
return 0;
}
```
该程序中,我们定义了两个队列queue1和queue2来模拟两个服务员。在每分钟的模拟中,如果有顾客到来,我们将其排到两个队列中排队;如果有服务员空闲,则从队首取出下一个顾客进行服务。在服务过程中,我们记录了总服务时间、完成服务的顾客数量和总等待时间,最后输出结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)