SharedSchoolSpace/可供参考资料_zyx/银行排队问题.cpp

272 lines
6.9 KiB
C++
Raw Normal View History

//<2F><><EFBFBD><EFBFBD>2-2
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
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<65><72>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int i, j = 0;//i<><69><EFBFBD>ڿͻ<DABF><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>j<EFBFBD><6A><EFBFBD>ڴ<EFBFBD><DAB4>ڼ<EFBFBD><DABC><EFBFBD>
int no = 0;//no<6E><6F><EFBFBD>ڼ<EFBFBD>¼<EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD>е<EFBFBD>һλ<D2BB>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ1
int time_length = 0;//time_length<74><68>¼ģ<C2BC><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
int window_number = 0;
typedef struct QNode
{
int no;//<2F><><EFBFBD><EFBFBD>
double amount;//<2F>ܽ<EFBFBD><DCBD><EFBFBD>
double remain_amount;//δȡ<CEB4><C8A1><EFBFBD><EFBFBD><EEA3A8><EFBFBD>Ƶ<EFBFBD>ǰ<EFBFBD>֣<EFBFBD>
struct QNode* next;
}QNode, * QueuePtr;
typedef struct
{
QueuePtr front; //<2F><>ͷָ<CDB7><D6B8>
QueuePtr rear; //<2F><>βָ<CEB2><D6B8>
}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)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȡ<EFBFBD>ꡣע<EAA1A3><D7A2><EFBFBD>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD>ա<EFBFBD>
{
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);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
cout << "<EFBFBD><EFBFBD>" << (double)i / 10 + 0.1 << "<EFBFBD><EFBFBD>ʱ" << j + 1 << "<EFBFBD>Ŵ<EFBFBD><EFBFBD><EFBFBD>"
<< "<EFBFBD><EFBFBD>" << e1 << "λ<EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>߹<EFBFBD>" << e2 << "<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>" << endl;
queue_number[j]--;
}
}
}
void Join(LinkQueue* q)
{
int a = rand() % 60;
//a<><61>ֵ<EFBFBD><D6B5>0-60֮<30><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD>ֻ<EFBFBD>е<EFBFBD>a=0<><30>window_number-1ʱ<31>Ų<EFBFBD><C5B2><EFBFBD><EFBFBD>¿ͻ<C2BF>
//ƽ<><C6BD>ÿ6<C3BF><36><EFBFBD><EFBFBD><EFBFBD><EFBFBD>window_number<65><72><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EBB4B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ
if (a < window_number)
{
no++;
double b = double(rand() % 191) / 10 + 1;//b<><62>ֵ<EFBFBD><D6B5>1-20֮<30><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD>(<28><><EFBFBD><EFBFBD>0.1)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>ȡǮ<C8A1>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
int m = MinQueue();
EnQueue(q[m], no, b, b);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);//<2F><><EFBFBD>ú<EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
cout << "<EFBFBD><EFBFBD>" << (double)i / 10 + 0.1 << "<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>" << no << "λ<EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"
<< m + 1 << "<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD>ȡ" << b << "<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>" << 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);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
cout << "<EFBFBD><EFBFBD>" << e1 << "λ<EFBFBD>ͻ<EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD><EFBFBD>" << max + 1 << "<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << min + 1 << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << 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<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>0.2<EFBFBD><EFBFBD>
}
}
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);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << j + 1 << "<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>" << e1 <<
"λ<EFBFBD>ͻ<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>δȡ<EFBFBD><EFBFBD>" << e2 << "<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>" << endl;
}
}
int main()
{
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1-300֮<30><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
//time_length = 120;
cin >> time_length;
while (time_length <= 0 || time_length > 300)
{
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
cin >> time_length;
}
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1-10֮<30><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
cin >> window_number;
while (window_number <= 0 || window_number > 10)
{
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
cin >> window_number;
}
LinkQueue* q = (LinkQueue*)malloc(window_number * sizeof(LinkQueue));
//<2F><><EFBFBD>г<EFBFBD>ʼ<EFBFBD><CABC>
for (j = 0;j < window_number;j++)
{
InitQueue(q[j]);
}
srand(time(nullptr));//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
for (i = 0;i <= time_length * 10;i++)//ÿ0.1<EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD>һ<EFBFBD>Σ<EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
{
//<2F>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȡ<EFBFBD>ꡣע<EAA1A3><D7A2><EFBFBD>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
for (j = 0;j < window_number;j++)
{
Remove(q[j]);
}
//<2F>ٰ<EFBFBD><D9B0>¿ͻ<C2BF><CDBB>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٵĶ<D9B5><C4B6><EFBFBD>
Join(q);
//<2F><><EFBFBD>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7>пͻ<D0BF><CDBB><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ChangeQuene(q);
//<2F>ٸ<EFBFBD><D9B8><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һλȡǮ<C8A1><C7AE>ע<EFBFBD><D7A2><EFBFBD>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
//ע<><D7A2>i=time_lengthʱȡǮҵ<C7AE><D2B5><EFBFBD>Ѿ<EFBFBD>ֹͣ
for (j = 0;j < window_number;j++)
{
Withdraw(q[j]);
}
Sleep(93);
//100<30><30>λΪ<CEBB><CEAA><EFBFBD><EFBFBD><EBA3AC>0.1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>100<EFBFBD><EFBFBD>Ϊ93
//<2F><><EFBFBD><EFBFBD>Sleep()<29><><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE>Ȳ<EFBFBD><C8B2>ߣ<EFBFBD><DFA3><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̫С
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δȡ<CEB4><C8A1>Ǯ<EFBFBD><C7AE><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD>
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>Ϊԭɫ
cout << endl << "ȡǮҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl << endl;
for (j = 0;j < window_number;j++)
{
Undone(q[j]);
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>Ϊԭɫ
return 0;
}