数据结构之单队列--银行排队系统

单队列–银行排队系统

项目名称:银行业务之单队列多窗口服务
1 实验目的
熟练掌握队列的基本操作,理解队列的应用。
2 实验内容
假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙。当有窗口空闲时,下一位顾客即去该窗口处理事务。当有多个窗口可选择时,假设顾客总是选择编号最小的窗口。
本题要求输出前来等待服务的N位顾客的平均等待时间、最长等待时间、最后完成时间,并且统计每个窗口服务了多少名顾客。
3 实验要求
(1)输入说明:输入第1行给出正整数 N(≤1000),为顾客总人数;随后N行,每行给出一位顾客的到达时间T和事务处理时间P,并且假设输入数据已经按到达时间先后排好了顺序;最后一行给出正整数K(≤10),为开设的营业窗口数。这里假设每位顾客事务被处理的最长时间为60分钟。
(2)输出说明:在第一行中输出平均等待时间(输出到小数点后1位)、最长等待时间、最后完成时间,之间用1个空格分隔,行末不能有多余空格。
在第二行中按编号递增顺序输出每个窗口服务了多少名顾客,数字之间用1个空格分隔,行末不能有多余空格。


代码


#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;

//定义队列的结点结构
typedef struct Qnode{
ElemType arrivetime;
ElemType Needtime;
struct Qnode *next;
} Qnode/结构体变量/, *Queueptr;/结构体指针变量/

//定义队列结构
typedef struct{
Queueptr front;
Queueptr rear;
}LinkQueue;

//初始化队列
void InitQueue(LinkQueue &Q){
Q.front=(Queueptr)malloc(sizeof(Qnode));//分配一个空间给头尾结点
Q.rear=Q.front;
}

//队列中插入元素传入队列,e1为到达时间,e2为需要的时间
void EnQueue(LinkQueue &Q,ElemType e1,ElemType e2)
{
Queueptr p= (Queueptr) malloc(sizeof(Qnode));
Q.rear->arrivetime=e1;
Q.rear->Needtime=e2;
Q.rear->next=p;
Q.rear=p;
}

//获取队头的元素,e1返回到达时间,e2返回需要的时间
void GetElem(LinkQueue &Q,ElemType &e1,ElemType &e2){
e1=Q.front->arrivetime;
e2=Q.front->Needtime;
}

//删除队头元素
void DeleteElem(LinkQueue &Q){
Q.front=Q.front->next;
}

// 判断队列是否为空
int Empty(LinkQueue &Q){
if(Q.front==Q.rear)
return 0;
else return 1;
}

int main(void){
int N; //N为顾客人数.
printf(“请输入顾客总人数:”);
scanf("%d",&N); //输入人数
LinkQueue Queue;
InitQueue(Queue); //初始化队列
for(int i=1;i<=N;i++){ //循环输入所有的顾客对应的到达时间,需要的时间,入队列
ElemType arrivetime,Needtime;
printf(“请输入客户到达时间:”);
scanf("%d",&arrivetime);
printf(“请输入客户办理业务时间:”);
scanf("%d",&Needtime);
EnQueue(Queue,arrivetime,Needtime); // 将对应的到达时间,需要的时间入队列
}

int K;//服务窗口的总数
printf(“请输入服务窗口个数:”);
scanf("%d",&K);
int wintime[10]={0},winnum[10]={0}; //wintime数组为每个窗口的完成时间,winnunm数组为每个窗口的服务人数
int sumwait=0,longestwait=0,onewait=0;
while(Empty(Queue))
{
//ifwait记录是否需要等待,mintime记录如果需要等待,则最快完成的窗口的时刻
int ifwait=0,mintime=100,flag;
ElemType arrivetime, Needtime;
//遍历各个窗口,如果到达时间比该窗口的完成时间大,则表示不需要等待
for (int i = 0; i <= K-1; i++)
{
GetElem(Queue, arrivetime, Needtime);
if (wintime[i] <= arrivetime)
{
wintime[i] = arrivetime +Needtime;
DeleteElem(Queue); //删除队头元素
winnum[i]++;
ifwait=1;
break; //表示不用排队
}

         if(mintime>wintime[i])
		  { 
            mintime = wintime[i];
            flag = i;//记录下该窗口的下标
          }
    }
if(!ifwait) { 
onewait = mintime - arrivetime; 
if (longestwait < onewait) longestwait = onewait;
sumwait = sumwait + onewait;
wintime[flag] = mintime + Needtime;
winnum[flag]++;  //对应窗口的服务人数加1
DeleteElem(Queue);  //删除队头元素
           }  

}

//最终完成时间
int overtime=0;
for(int i=0;i<=K-1;i++){ // 遍历求出wintime数组中最大的一个,即为最终的完成时间
if(overtime<wintime[i]) overtime=wintime[i];
}
printf(“平均等待时间%.1lf 最长等待时间%d 最后完成时间%d\n”, 1.0 *sumwait/9,longestwait,overtime);//输出,平均等待时间, 最长等待时间, 最后完成时间
for(int i=0;i<=K-1;i++){
printf(“输出各个窗口的服务人数%d”, winnum[i]);//输出各个窗口的人数
if(i == K - 1) printf("\n");
else printf(" ");
}
}

  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Recently 祝祝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值