1017 Queueing at Bank 模拟银行排队之等待时间==时间之差

题目

在这里插入图片描述

分析

题意:让我们求出平均等待时间,题目给出办理人数和窗口数,银行营业的时间为早上8点到晚上17点,17点以后不再服务(17点后不算入办理人数),现给出每个顾客来的时间和办理业务的时长,每个窗口一次只能办一人的业务,根据此规则来计算平均等待时间。

解题过程

  1. 关键是计算出每个人的等待时间,这个等待时间实际上就是窗口办理上一个人的结束时间与等待人的到达时间之差,所以把所有人的等待时间加起来除以人数就是平均等待时间啦。
  2. 处理时间:因为题目是以hh:mm:ss的形式给出时间,为了方便计算,应该转换为以秒为单位的时间。
  3. 时间处理完后,我们可以按时间到达次序排序,window数组表示 某个窗⼝的结束时间,每⼀个客户到来的时候,选择早结束时间的窗⼝,如果早结束时间比他还早,那么他⼀来就能被服务,更新window的值;如果早结束时间⽐他晚,他需要等待,累加等待的时间,然后更新window的值。
  4. 请看代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
	int come, time;
} tempcustomer;
bool cmp1(node a, node b)
{
	return a.come < b.come;
}
int main()
{
	int n, k;
	scanf("%d%d", &n, &k);
	vector<node> custom;
	for(int i = 0; i < n; i++)
	{
		int hh, mm, ss, time;
		scanf("%d:%d:%d %d", &hh, &mm, &ss, &time);
		int cometime = hh * 3600 + mm * 60 + ss;
		if(cometime > 17*3600) continue;
		tempcustomer = {cometime, time*60};
		custom.push_back(tempcustomer);
	}
	sort(custom.begin(), custom.end(), cmp1);
	vector<int> window(k, 8*3600);
	double result = 0.0;
	for(int i = 0; i < custom.size(); i++)
	{
		int tempindex = 0, minfinish = window[0];
		for(int j = 1; j < k; j++)
		{
			if(minfinish > window[j])
			{
				minfinish = window[j];
				tempindex = j;
			}
		}
		if(window[tempindex] <= custom[i].come)
		{
			window[tempindex] = custom[i].come + custom[i].time;
		}
		else
		{
			result += (window[tempindex] - custom[i].come);
			window[tempindex] += custom[i].time;
		}
	}
	if(custom.size() == 0) printf("0.0");
	else printf("%.1f", result/60.0/custom.size());
	return 0;
}

小结

  1. vector赋值创建操作:vectorname(num, value);

感谢阅读

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设某银行有n个窗口对外接待客户,从早晨银行9点开门起到5点关门,不断有客户进入银行,由于每个窗口在某个时刻只能接待一个客户。因此在客户人数众多时需要在每个窗口前顺次排队,对于刚进银行的客户。如果某个窗口的业务员正空闲,则可上前输业务。反之,若个窗口均有客户所占,他便会排在为数最少的队伍后面。编制一个程序模拟银行的这种业务活动并计算一天中客户在银行的平均逗留时间。 首先从题目分析:N个窗口排队,首先就要建立N个队列来存储排队的用户信息 ,然后算出那个队列最短就用户就到那个队伍排队,同时通过随机生成他办理业务的时间和到来的时间,通过计算用户的到来时间和离开时间就可以计算出某个用户在银行的逗留时间 ;话不多说直接上代码。 下面是主函数,由用户输入银行上下班时间,计算营业多长时间Total_time,如何当前时间小于关门的时间,就一直进入customer_into();函数,用户不断的进来 #define FALSE 0 #define TRUE 1 #define QUEUE_SUM 4 //窗口的数量 int rand_business_time=0, rand_wait_time=0;//定义办理时间等待时间变量 int Total_time=0,now_tim=0;//总时间,当前时间 int go_time[4] = {0,0,0,0};//定义数组存储每个窗口最后一位办理完业务的时间 int sum_nan[4] = {0,0,0,0};//定义数组存储每个窗口排队的人数 int Sign=TRUE; //是否关门标志位 float Sum_Wait_Time=0.0; //等待的总时间 float Sun_Nan=0.0; //总人数 int open_time;//开门时间 int off_time; //关门时间 int main() { Prompted(); printf("输入银行的24小时制营业时间:如营业时间为9:00--17:00,则应输入:9,17\n"); scanf("%d,%d", &open;_time,&off;_time); Total_time = (off_time - open_time) * 60;//计算银行总营业多少分钟 for (int i = 0; i now_time) { customer_into(); //客户进入函数 } printf("银行关门时间到不再接收客人\n\n"); for (int i = 0; i < QUEUE_SUM; i++) { DisposeQueue(&queue;[i],i);//输入在银行关门前还没有办理完业务的客户信息 } printf("平均时间为%.2f分钟",Sum_Wait_Time/Sun_Nan); /*通过各个客户的总等待时间总和/总人数算出逗留平均时间*/ _getch(); return 0; }
Queueing theory is a mathematical study of waiting lines or queues that arise in various real-life scenarios, such as customer service, traffic congestion, hospital emergency rooms, and telecommunications networks. Basic queueing theory involves the following concepts: 1. Arrival Process: This is the process of customers arriving at the queue. The arrival process can be modeled using different distributions, such as Poisson or exponential. 2. Service Process: This is the process of serving customers in the queue. The service process can also be modeled using different distributions, such as Poisson or exponential. 3. Queue Length: This is the number of customers waiting in the queue at any given time. 4. Queue Occupancy: This is the proportion of time that the server is busy serving customers. 5. System Capacity: This is the maximum number of customers that the system can handle at any given time. 6. Utilization: This is the proportion of time that the server is busy serving customers compared to the total time. 7. Waiting Time: This is the time that a customer spends waiting in the queue before being served. 8. Service Time: This is the time that a customer spends being served by the server. 9. Queueing Models: There are different queueing models that can be used to analyze queueing systems, such as the M/M/1 model, M/M/c model, M/G/1 model, and M/D/1 model. 10. Performance Measures: Different performance measures can be used to evaluate queueing systems, such as average waiting time, average queue length, and system throughput.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值