#include<stdio.h>
#include<stdlib.h>
#define MaxWindow 30
#define MaxProc 1
#define ERROR -1
struct People
{
int T;
int P;
};
typedef struct People ElementType;
struct QNode
{
ElementType *Data;
int Front, Rear;
int MaxSize;
};
typedef struct QNode *Queue;
Queue CreatQueue(int MaxSize)
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
Q->Front = Q->Rear = 0;
Q->MaxSize = MaxSize;
return Q;
}
bool IsFull(Queue Q)
{
return((Q->Rear + 1) % Q->MaxSize == Q->Front);
}
bool AddQ(Queue Q, ElementType X)
{
if (IsFull(Q))
{
printf("队列满\n");
return false;
}
else
{
Q->Rear = (Q->Rear + 1) % Q->MaxSize;
Q->Data[Q->Rear] = X;
return true;
}
}
bool IsEmpty(Queue Q)
{
return(Q->Front == Q->Rear);
}
ElementType DeleteQ(Queue Q)
{
ElementType data;
if (IsEmpty(Q))
{ printf("队列空");
return ERROR;
}
else
{
Q->Front = (Q->Front + 1) % Q->MaxSize;
return Q->Data[Q->Front];;
}
}
void DestroyQueue(Queue Q)
{
free(Q);
}
int FindNextWindow(int W[], int k, int *WaitTime)
{
int WinAvail;
int MinW = MaxProc + 1;
int i;
for (i = 0; i < k; i++)
if (W[i] < MinW)
{
MinW = W[i];
WinAvail = i;
}
*WaitTime = MinW;
for (i = 0; i < k; i++)
W[i] -= MinW;
return WinAvail;
}
double QueueingAtBank(Queue Q, int N)
{
struct People Next;
int k;
int TotalTime;
int CurrentTime;
int Window[MaxWindow];
int WaitTime;
int WinAvail;
int i, j;
scanf("%d", &k);
for (i = 0; i < k; i++)
Window[i] = 0;
TotalTime = 0;
while (!IsEmpty)
{
WinAvail = FindNextWindow(Window, k, &WaitTime);
CurrentTime += WaitTime;
Next = DeleteQ(Q);
if (CurrentTime >= Next.T)
TotalTime += (CurrentTime - Next.T);
else
{
WaitTime = Next.T - CurrentTime;
for (j = 0; j < k; j++)
{
Window[j] -= WaitTime;
if (Window[j] < 0)
Window[j] = 0;
}
CurrentTime = Next.T;
}
Window[WinAvail] = Next.P;
}
return((double)TotalTime / (double)N);
}
int main()
{
int N;
Queue Q;
int i;
ElementType X;
scanf("%d", &N);
Q = CreatQueue(N);
for (i = 0; i < N; i++)
{
scanf("%d %d", &X.T, &X.P);
AddQ(Q, X);
}
printf("Average waiting time = %.lf minute(s).\n", QueueingAtBank(Q, N));
DestroyQueue(Q);
return 0;
}
循环队列 银行排队疑问?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- qq_34495994 2019-06-27 11:41关注
Elementtype比其他基础类型更有利于阅读程序的需要,可以定义成多种类型,本例中返回值类型与你定义的Elementtype类型不一致,故报错。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥30 这是哪个作者做的宝宝起名网站
- ¥60 版本过低apk如何修改可以兼容新的安卓系统
- ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
- ¥50 有数据,怎么建立模型求影响全要素生产率的因素
- ¥50 有数据,怎么用matlab求全要素生产率
- ¥15 TI的insta-spin例程
- ¥15 完成下列问题完成下列问题
- ¥15 C#算法问题, 不知道怎么处理这个数据的转换
- ¥15 YoloV5 第三方库的版本对照问题
- ¥15 请完成下列相关问题!