PTA 7-3 银行排队问题之单队列多窗口服务

 假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙。当有窗口空闲时,下一位顾客即去该窗口处理事务。当有多个窗口可选择时,假设顾客总是选择编号最小的窗口。

本题要求输出前来等待服务的N位顾客的平均等待时间、最长等待时间、最后完成时间,并且统计每个窗口服务了多少名顾客。

输入格式:

输入第1行给出正整数N(≤1000),为顾客总人数;随后N行,每行给出一位顾客的到达时间T和事务处理时间P,并且假设输入数据已经按到达时间先后排好了顺序;最后一行给出正整数K(≤10),为开设的营业窗口数。这里假设每位顾客事务被处理的最长时间为60分钟。

输出格式:

在第一行中输出平均等待时间(输出到小数点后1位)、最长等待时间、最后完成时间,之间用1个空格分隔,行末不能有多余空格。

在第二行中按编号递增顺序输出每个窗口服务了多少名顾客,数字之间用1个空格分隔,行末不能有多余空格。

输入样例:

9
0 20
1 15
1 61
2 10
10 5
10 3
30 18
31 25
31 2
3

输出样例:

6.2 17 61
5 3 1
#include<iostream>
#include<string>
#include<iomanip>
#include<queue>
using namespace std;
struct Servers {
	int count = 0;
	int donetime=0;
};
int main()
{
	Servers server[100] = { 0 };//代表服务窗口
	int Maxwaittime =0;
	double allwaittime=0;
	int onewaittime=0;
	int n, m;
	int No1, time,maxx=0;
	int min;
	int key,flag=0;
	queue<int> ct;
	queue<int> nt;
	cin >> n;//人
	for (int i = 1; i <= n; i++)//数据入队
	{
		cin >> No1 >> time;
		if (time > 60)
			time=60;
		ct.push(No1);
		nt.push(time);
	}
	cin >> m;//窗口数
	for (int i = 1 ; i <= n; i++)
	{
		flag = 0;//必须重置
		for (int j = 1; j <= m; j++)
		{
			if (server[j].donetime <= ct.front())//窗口等人,人来的晚
			{
				//cout<<"++++"<<j<<endl;
				server[j].donetime = ct.front()+ nt.front();//数据更新
				server[j].count++;
				ct.pop();
				nt.pop();
				flag = 1;
				break;
			}	
		}
		if(flag!=1)//人必须等
		{
			min = server[1].donetime;
			key = 1;
			for (int j = 1; j <= m; j++)
			{
				if (server[j].donetime < min)
					min = server[j].donetime;
			}
			for (int j = 1; j <= m; j++)
			{
				if (server[j].donetime == min)
				{
					key = j;
					break;
				}
			}
			//cout << "------" << key << endl;
			onewaittime = server[key].donetime - ct.front();
			allwaittime += onewaittime;
			if (onewaittime > Maxwaittime)
				Maxwaittime = onewaittime;
			server[key].donetime += nt.front();//数据更新
			server[key].count++;
			ct.pop();
			nt.pop();
		}
	}
    maxx=server[1].donetime;
    for(int i=1;i<=m;i++)
    {
      if(server[i].donetime>maxx)
          maxx=server[i].donetime;
    }
	cout  << fixed<<setprecision(1)<<1.0*allwaittime/n<<" ";
	cout << Maxwaittime << " ";
	cout << maxx << endl;
    for(int i=1;i<=m-1;i++)
    {
      cout<< server[i].count<< " "  ;
    }
	cout<< server[m].count<<endl;

	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值