From 18028bd83d7c3221aa4fffd20ceb148dccd500d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E5=AE=87=E7=AC=91?= Date: Fri, 3 Nov 2023 11:05:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'=E5=8F=AF=E4=BE=9B=E5=8F=82=E8=80=83=E8=B5=84=E6=96=99=5Fzy?= =?UTF-8?q?x'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 可供参考资料_zyx/银行排队问题.cpp | 272 ++++++++++++++++++++++++++++++ 1 file changed, 272 insertions(+) create mode 100644 可供参考资料_zyx/银行排队问题.cpp diff --git a/可供参考资料_zyx/银行排队问题.cpp b/可供参考资料_zyx/银行排队问题.cpp new file mode 100644 index 0000000..6c25334 --- /dev/null +++ b/可供参考资料_zyx/银行排队问题.cpp @@ -0,0 +1,272 @@ +//2-2 +#include +#include +#include +#include +#include + +using namespace std; + +#define Status int +#define OVERFLOW 2 +#define ERROR 1 +#define OK 0; +typedef int QElemType; + +int queue_number[10] = { 0 };//queue_number¼ +int i, j = 0;//iڿͻjڴڼ +int no = 0;//noڼ¼ͻţеһλͻΪ1 +int time_length = 0;//time_length¼ģʱ +int window_number = 0; + +typedef struct QNode +{ + int no;// + double amount;//ܽ + double remain_amount;//δȡƵǰ֣ + struct QNode* next; +}QNode, * QueuePtr; + +typedef struct +{ + QueuePtr front; //ͷָ + QueuePtr rear; //βָ +}LinkQueue; + +Status InitQueue(LinkQueue& Q) +{ + Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode)); + if (!Q.front) exit(OVERFLOW); + Q.front->next = NULL; + return OK; +} + +Status DestroyQueue(LinkQueue& Q) +{ + while (Q.front) + { + Q.rear = Q.front->next; + free(Q.front); + Q.front = Q.rear; + } + return OK; +} + +Status QueueEmpty(LinkQueue Q) +{ + return (Q.front == Q.rear); +} + +Status GetHead(LinkQueue Q, int& e1, double& e2, double& e3) +{ + if (Q.front == Q.rear) return ERROR; + e1 = Q.front->next->no; + e2 = Q.front->next->amount; + e3 = Q.front->next->remain_amount; + return OK; +} + +Status EnQueue(LinkQueue& Q, int& e1, double& e2, double& e3) +{ + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) exit(OVERFLOW); + p->no = e1; + p->amount = e2; + p->remain_amount = e3; + p->next = NULL; + Q.rear->next = p; + Q.rear = p; + return OK; +} + +Status DeQueue(LinkQueue& Q, int& e1, double& e2, double& e3) +{ + if (Q.front == Q.rear) return ERROR; + QueuePtr p; + p = Q.front->next; + e1 = p->no; + e2 = p->amount; + e3 = p->remain_amount; + Q.front->next = p->next; + if (Q.rear == p) Q.rear = Q.front; + delete p; + return OK; +} + +int MinQueue() +{ + int s = 10000, k = 0, temp; + for (k = 0;k < window_number;k++) + { + if (queue_number[k] < s) + { + temp = k; + s = queue_number[k]; + } + } + return temp; +} + +int MaxQueue() +{ + int s = -1, k = 0, temp; + for (k = 0;k < window_number;k++) + { + if (queue_number[k] > s) + { + temp = k; + s = queue_number[k]; + } + } + return temp; +} + +void Remove(LinkQueue& Q)//һǷȡꡣעж϶ǷΪա +{ + if (!QueueEmpty(Q)) + { + if (Q.front->next->remain_amount <= 1e-8) + { + int e1 = -1; + double e2 = -1, e3 = -1; + DeQueue(Q, e1, e2, e3); + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), + FOREGROUND_INTENSITY | FOREGROUND_GREEN);//ɫ + cout << "" << (double)i / 10 + 0.1 << "ʱ" << j + 1 << "Ŵ" + << "" << e1 << "λͻҵȡ߹" << e2 << "Ԫ" << endl; + queue_number[j]--; + } + } +} + +void Join(LinkQueue* q) +{ + int a = rand() % 60; + //aֵ0-60ֲֻ֮еa=0window_number-1ʱŲ¿ͻ + //ƽÿ6window_numberͻ봰Ӧ + if (a < window_number) + { + no++; + double b = double(rand() % 191) / 10 + 1;//bֵ1-20ֲ֮(0.1)ûȡǮĽλԪ + int m = MinQueue(); + EnQueue(q[m], no, b, b); + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), + FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);//úɫɫ + cout << "" << (double)i / 10 + 0.1 << "ʱ" << no << "λͻ봰" + << m + 1 << "Уȡ" << b << "Ԫ" << endl; + queue_number[m]++; + } +} + +void ChangeQuene(LinkQueue* q) +{ + int min = MinQueue(), max = MaxQueue(); + while (queue_number[max] - queue_number[min] >= 2) + { + int e1; + double e2, e3; + DeQueue(q[max], e1, e2, e3); + EnQueue(q[min], e1, e2, e3); + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), + FOREGROUND_INTENSITY | FOREGROUND_BLUE);//ɫ + cout << "" << e1 << "λͻӴ" << max + 1 << "е" << min + 1 << "" << endl; + + queue_number[min]++; + queue_number[max]--; + min = MinQueue(); + max = MaxQueue(); + } +} + +void Withdraw(LinkQueue& Q) +{ + if (!QueueEmpty(Q) && i != time_length) + { + Q.front->next->remain_amount -= 0.2;//ÿ0.1ȡ0.2 + } +} + +void Undone(LinkQueue& Q) +{ + while (!QueueEmpty(Q)) + { + int e1 = -1; + double e2 = -1, e3 = -1; + DeQueue(Q, e1, e2, e3); + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), + FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);//ɫɫ + cout << "" << j + 1 << "е" << e1 << + "λͻδҵδȡ" << e2 << "Ԫ" << endl; + } +} + +int main() +{ + cout << "ģʱλ룩1-300֮" << endl; + //time_length = 120; + cin >> time_length; + while (time_length <= 0 || time_length > 300) + { + cout << "ʱ䲻Ҫ룺"; + cin >> time_length; + } + + cout << "д1-10֮" << endl; + cin >> window_number; + while (window_number <= 0 || window_number > 10) + { + cout << "ĴҪ룺"; + cin >> window_number; + } + + LinkQueue* q = (LinkQueue*)malloc(window_number * sizeof(LinkQueue)); + + //гʼ + for (j = 0;j < window_number;j++) + { + InitQueue(q[j]); + } + + srand(time(nullptr));// + + //Ĵ + for (i = 0;i <= time_length * 10;i++)//ÿ0.1ѭһΣֱԤʱ + { + //ȼһǷȡꡣעж϶ǷΪ + for (j = 0;j < window_number;j++) + { + Remove(q[j]); + } + + //ٰ¿ͻӵٵĶ + Join(q); + + //жǷпͻҪѡ + ChangeQuene(q); + + //ٸǰһλȡǮעж϶ǷΪ + //עi=time_lengthʱȡǮҵѾֹͣ + for (j = 0;j < window_number;j++) + { + Withdraw(q[j]); + } + + Sleep(93); + //100λΪ룬0.1롣ǵʱ䣬100Ϊ93 + //Sleep()ľȲߣѭ̫С + } + + //ǰδȡǮ˵ + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), + FOREGROUND_INTENSITY);//ɫûɫΪԭɫ + cout << endl << "ȡǮҵ" << endl << endl; + for (j = 0;j < window_number;j++) + { + Undone(q[j]); + } + + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), + FOREGROUND_INTENSITY);//ɫûɫΪԭɫ + return 0; +} \ No newline at end of file