PAT(甲级)1017笔记
1. 原题参照PAT官网
1017
希望一个银行,有K个服务窗口。有一条黄线在设备窗口前,把等候区分为两部分。
所有的顾客都需要在黄线之后等待,直到有一个窗口空闲并且轮到他/她上前去办理业务
Assumed 窗口不能被一个顾客占据超过1小时。
现在有每个顾客有arriving time T和processing time P,你被期望告诉他们每位顾客的平均等待时间。
输入:
第一行有两个数字
N位顾客 K个窗口
接下来有n行,每行有该顾客的两个时间HH:MM:SS arriving time 和P processing time。
HH范围【0-23】MM和SS范围【0-59】同一个顾客不可能在同一个时间到来。
注意银行营业时间是8:00-17:00 所以八点开始才有顾客到来等待,5点后到来的顾客不会被服务。nor counted into the average.(也不计入平均值)
输出:
在一行输出顾客的平均等待值,in minutes and accurate up to 1 decimal place.(以分钟为单位,精确到小数点后1位。)
2.知识点
- 第一个问题在思路,K个窗口如何调用。每次都挑选最早的那个窗口就可以了。
- 第二个问题:输出的时候遇到的问题!!!
cout << setprecision(2) << average;
//上面那个只是看起来像,但下面那个是正确的答案格式。我疯了,居然因为这个小东西坑了半天。
printf("%.1f", average);
2. 代码record
#include<stdio.h>
#include<string>
#include<queue>
#include<stack>
#include<iostream>
#include<vector>
#include <iomanip>
#include<algorithm>
#include<map>
using namespace std;
#define MAXSIZE 30
struct customer {
int arrivingTime;
int processingTime;
};
bool cmp1(customer a,customer b){
return a.arrivingTime < b.arrivingTime;
}
int main() {
int N, K;
cin >> N >> K;
vector<customer> cus;
//输入客户数据
for (int i = 0; i < N; i++){
int tempH, tempM, tempS, tempM2;
scanf("%2d:%2d:%2d %d", &tempH, &tempM, &tempS, &tempM2);
customer temp;
temp.arrivingTime = tempH * 60 * 60 + tempM * 60 + tempS;
temp.processingTime = tempM2 * 60;
if (temp.arrivingTime <=(17 * 60 * 60))
cus.push_back(temp);
}
sort(cus.begin(),cus.end(), cmp1);
//按照抵达时间排序
vector<int>windows(K, (8 * 60 * 60));
double average=0.0;
for (int i = 0; i < cus.size(); i++)
{
int theEarly=0;
//找到最小的那个时间
for (int j = 1; j < K; j++)
theEarly = windows[theEarly] <= windows[j] ? theEarly : j;
//若刚好有kong
if (windows[theEarly] <= cus[i].arrivingTime) {
windows[theEarly] = cus[i].arrivingTime + cus[i].processingTime;
}
//若刚好要等
else {
//if (cus[i].arrivingTime > (8 * 60 * 60))
average += (windows[theEarly] - cus[i].arrivingTime);
windows[theEarly] += cus[i].processingTime;
}
}
average /= 60;
average /= cus.size();
if (cus.size() == 0) { cout << "0.0"; return 0; }
//cout << setprecision(2) << average;
//上面那个只是看起来像,但下面那个是正确的答案格式。我疯了,居然因为这个小东西坑了半天。
else
printf("%.1f", average);
return 0;
}