[ PAT-A ] Queueing at Bank (C++)

题目描述

Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.
Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.

Input Specification:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=10000) - the total number of customers, and K (<=100) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS - the arriving time, and P - the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time.
Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.

Output Specification:
For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.

Sample Input:
7 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10

Sample Output:
8.2


解题思路

题目大意
银行开通K个窗口,为N个人进行服务,每个窗口一次只能为1个人服务,其他人按先来后到排队进行等候,你所需要计算的是,平均每个人得等待多长时间.注意银行工作时间为8:00~17:00.
思路

  • 如果窗口有空闲的话,将不需要等待,即该窗口上一个服务对象结束时间在另一人到达银行排队之前,否则的话,上一人服务结束时间减去本次服务对象到达银行时间,即为等待时长
  • 将每个人的等待时长相加,即为所有人的等待总时长

代码设计
//结构体设计
//zhicheng
typedef struct{string time;float pro;}D;

[任务] 计算每个人的服务时长
[说明]
判断服务结束时间是否早于顾客到来时间,早的话,无需等待,否则的话,记下两时间差,即为该顾客等待时长
[接口]
float op(D a,vector<D>& win)
输入: a 顾客状况(到达时间和服务时长)win窗口状态(服务结束时间和无用变量)
输出:顾客本次等待时长

//代码实现
//zhicheng
float op(D a,vector<D>& win)
{
    float time=0;
    sort(win.begin(),win.end(),comp);
    if(win[0].time>a.time)
    {
        time=op_SubStrTime(win[0].time,a.time);
        a.time=win[0].time;
    }
    win[0].time=op_AddStrTime(a.time,a.pro);
    //cout<<a.time<<" "<<a.pro<<endl;
    //cout<<a.time<<" "<<time<<endl;
    return time;
}

[任务] 计算两时间差
[说明]
两字符串对应量相减后,得出时间差,单位为分
[接口]
float op_SubStrTime(string start,string end)
输入: start 结束时间,end 开始时间
输出:两时间差

//代码实现
//zhicheng
float op_SubStrTime(string start,string end)
{
    int t1[3]={0},t2[3]={0},cn1=0,cn2=0;
    for(int i=0;i<start.length();i++){if(start[i]==':'){cn1++;i++;} t1[cn1]=t1[cn1]*10+start[i]-'0';}
    for(int i=0;i<end.length();i++){if(end[i]==':'){cn2++;i++;} t2[cn2]=t2[cn2]*10+end[i]-'0';}
    return ((t1[0]-t2[0])*60*60+(t1[1]-t2[1])*60+(t1[2]-t2[2]))*1.0/60;
}

[任务] 计算某时间点过了p分钟后的时间点
[接口]
string op_AddStrTime(string star,float p)
输入: star 开始时间,p表示star过了p分钟
输出:star过了p分钟后的时间

//代码实现
//zhicheng
string op_AddStrTime(string star,float p)
{
    int t[3]={0},cn=0;
    for(int i=0;i<star.length();i++){if(star[i]==':'){cn++;i++;} t[cn]=t[cn]*10+star[i]-'0';}
    int time=t[0]*60*60+t[1]*60+t[2]+(int)p*60;
    t[2]=time%60;time=time/60;
    t[1]=time%60;time=time/60;
    t[0]=time;
    star.clear();
    star.push_back(t[0]/10+'0');star.push_back(t[0]%10+'0');star.push_back(':');
    star.push_back(t[1]/10+'0');star.push_back(t[1]%10+'0');star.push_back(':');
    star.push_back(t[2]/10+'0');star.push_back(t[2]%10+'0');
    return star;
}


有关PAT (Basic Level) 的更多内容可以关注 ——> PAT-B题解


有关PAT (Advanced Level) 的更多内容可以关注 ——> PAT-A题解

铺子日常更新,如有错误请指正
传送门:代码链接 PTA题目链接 牛客网题目链接 PAT-A题解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值