pat甲级1017(25分)c++
这道题是pat甲级1014的简单版本,建议先写这道题,然后再写pat甲级1014
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
* 题目大意:
* 输入:
* 第一行:
* N为客户总数,K为窗口数量
* 接下来N行为客户的到达时间和过程时间
* 输出:
* N个客户的平均等待时间
*/
/*
* 结构体node用于存储客户数据
*/
struct node {
int come, time;
} tempcustomer;
/*
* cmp1为排序规则
*/
bool cmp1(node a, node b) {
return a.come < b.come;
}
int main() {
//初始化
int n, k;
scanf("%d%d", &n, &k);
//custom用于存储node
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 > 61200) continue; //61200为17:00的秒数,用于判断到达时间是否在营业时间
tempcustomer = {cometime, time * 60};
custom.push_back(tempcustomer);
}
//对数据进行排序
sort(custom.begin(), custom.end(), cmp1);
vector<int> window(k, 28800);
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;
}
这道题主要考察的是对于数据最小值的查找,并不是很难,相对于pat甲级1014,甚至不需要使用queue,仅仅需要使用vector存储一下窗口结束时间
PAT甲级1017是一道简单的数据结构题目,重点在于查找数据的最小值。相比1014题,1017题难度较低,不需要使用队列,仅需通过vector来存储窗口结束时间即可完成。
1697

被折叠的 条评论
为什么被折叠?



