假設不存在插隊的情況,並且人人都會選擇排到人數最少的那一個視窗排隊。假設人們陸續到達銀行,不會同時到達銀行。
程式碼如下:
#include<iostream>
#include<ctime>
#include<stdlib.h>
#define MAX_SIZE 10000//佇列的初始化長度
#define END_TIME 10000//銀行關門時間
using namespace std;
typedef struct TIME {//
int daoda_time;//客戶到達時間
int likai_time;//客戶離開時間
int banli_time;//客戶辦理業務所需時間
}time1;
typedef struct Windows {
TIME* point;
int front;//記入佇列的隊頭位置
int rear;//即入佇列的隊尾位置
int size;//定義的線表可以使用的長度
int length;//已經使用的長度
}window;
window list1, list2, list3, list4;
int n1, n2, n3, n4;//計時器
int nn;//計時器
int all_time;
int people_num;
int record1;//計入亂數,記錄當前到達的客戶辦理業務所需的時間
int record2;//記入亂數,記入下一客戶到達的間隔時間
void moni_daoda(int e);
int search_min();
void banli_go(window* a, int b, int& c);
int main()
{
srand(time(0));//設定亂數種子
record1 = rand() % 5 + 5;
record2 = rand() % 3 + 1;
cout << record1 << " " << record2 << endl;
list1.point = (TIME*)malloc(MAX_SIZE * sizeof(TIME));//初始化四個視窗佇列
list1.size = MAX_SIZE; list1.front = 0;
list1.point[0].daoda_time = 0; list1.point[0].banli_time = record1;
list1.rear = 1; list1.length = 1;
people_num = 1;
list2.point = (TIME*)malloc(MAX_SIZE * sizeof(TIME));
list2.front = 0; list2.length = 0; list2.rear = 0; list2.size = MAX_SIZE;
list3.point = (TIME*)malloc(MAX_SIZE * sizeof(TIME));
list3.front = 0; list3.length = 0; list3.rear = 0; list3.size = MAX_SIZE;
list4.point = (TIME*)malloc(MAX_SIZE * sizeof(TIME));
list4.front = 0; list4.length = 0; list4.rear = 0; list4.size = MAX_SIZE;
for (int n = 0; n <= END_TIME; n++)
{
moni_daoda(n);
if (list1.length != 0) banli_go(&list1, n, n1);
if (list2.length != 0) banli_go(&list2, n, n2);
if (list3.length != 0) banli_go(&list3, n, n3);
if (list4.length != 0) banli_go(&list4, n, n4);
}
cout << "平均每個客戶在銀行的逗留時間為:" << all_time / people_num << endl;
return 0;
}
void moni_daoda(int e)模擬客戶到達銀行,並且尋找視窗排隊。
{
int i;
if (nn < record2)
{
nn++;
}
else//到達間隔時間後新的客戶到達
{
record1 = rand() % 5 + 5;
record2 = rand() % 3 + 1;
i = search_min();//尋找人數最小的哪一個視窗
if (i == 1)//客戶到達後的選視窗入隊操作
{
list1.point[list1.rear].daoda_time = e; list1.point[list1.rear].banli_time = record1;
list1.length = list1.length + 1; list1.rear = list1.rear + 1;
people_num++;
}
else if (i == 2)
{
list2.point[list2.rear].daoda_time = e; list2.point[list2.rear].banli_time = record1;
list2.length = list2.length + 1; list2.rear = list2.rear + 1;
people_num++;
}
else if (i == 3)
{
list3.point[list3.rear].daoda_time = e; list3.point[list3.rear].banli_time = record1;
list3.length = list3.length + 1; list3.rear = list3.rear + 1;
people_num++;
}
else
{
list4.point[list4.rear].daoda_time = e; list4.point[list4.rear].banli_time = record1;
list4.length = list4.length + 1; list4.rear = list4.rear + 1;
people_num++;
}
nn = 0;
}
}
int search_min()//尋找人數最小的那一個視窗的函數
{
if (list1.length >= list2.length || list1.length >= list3.length || list1.length >= list4.length) return 1;
else if (list2.length > list1.length || list2.length >= list3.length || list2.length >= list4.length) return 2;
else if (list3.length > list1.length || list3.length > list2.length || list3.length >= list4.length) return 3;
else return 4;
}
void banli_go(window* a, int b, int& c)//客戶辦理好業務後的出隊操作
{
if (c < a->point[a->front].banli_time)
{
c++;
}
else//出隊操作
{
a->point[a->front].likai_time = b;
all_time = all_time + a->point[a->front].likai_time - a->point[a->front].daoda_time;
a->length = a->length - 1;
a->front = a->front + 1;
c = 0;
}
}