题目链接
https://pintia.cn/problem-sets/994805342720868352/problems/994805493648703488
题目考点
map、排序
题目难度
PAT甲级25分
题目大意
输入
cents/minute
)MM:dd:HH:mm
)、1个词(on-line
或off-line
),这些通话记录都是同一个月的。1个on-line
记录与同1客户的时间顺序上的1个off-line
记录配对,没有形成pair的通话记录会被忽略。输入中至少会有1个pair。假设同1客户不会有2条时间相同的记录。输入中时间的格式为24小时制。输出
按客户名称的字母表顺序为每1个客户输出账单。对于每1个客户来说,要输出其名字、账单的月份、每条通话记录(包括开始时间、结束时间、通话分钟数、收费金额,要按时间顺序输出多条通话记录)、账单总金额
找到pair,生成通话记录
先根据时间排序,然后判断是否形成pair(时间顺序、同用户、onLine和offLine)
计算1次通话的费用,还有总费用
可以从0开始计算(需要先对收费金额进行处理),这样计算更容易。on-line
和off-line
2个时间点,计算0点到这2个时间点一直通话的收费,取差额即为应该支付的费用。
确定月份
输出时取第1个记录的月份
客户名字字典序
map实现(也可以通过结构体排序实现)
Record时间序输出
通过结构体排序实现
读取输入时不一定要保存原信息
比如时间本身是string,我们可以直接记录month
、day
等,把:
等分隔符去除。on-line
和off-line
是字符串,我们可以用bool表示。
// Problem: PAT Advanced 1016
// URL: https://pintia.cn/problem-sets/994805342720868352/problems/994805493648703488
// Tags: map 排序
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int charge[25];
struct Record{
string name;
bool status;
int month, day, hour, minute, time;
void setStatus(string& status){
if (status == "on-line")
this->status = true;
else
this->status = false;
}
void calcTime(){ // 以月初为零点的时间
time = day * 24 * 60 + hour * 60 + minute;
}
bool pairWith(Record& r){
return name == r.name && status==true && r.status==false;
}
double calcCostFromZero(){
double cost = day * 60 * charge[24] + minute * charge[hour]; // 天、分钟
for (int i=0; i<hour; i++){ // 小时
cost += 60 * charge[i];
}
return cost / 100.0; // cent换算成美元
}
void print(){
printf("%02d:%02d:%02d ", day, hour, minute);
}
};
bool recordCmp(Record& a, Record& b){
return a.name == b.name ? a.time < b.time : a.name < b.name;
}
int main()
{
// 读取输入并处理数据
int n;
for (int i=0; i<24; i++){
scanf("%d", charge + i);
charge[24] += charge[i]; // charge[24]是1整天都在通话时1天的收费
}
scanf("%d", &n);
vector<Record> records(n);
string status;
for (int i=0; i<n; i++){
cin >> records[i].name;
scanf("%d:%d:%d:%d", &records[i].month, &records[i].day, &records[i].hour, &records[i].minute);
cin >> status;
records[i].setStatus(status);
records[i].calcTime(); // 以月初为0点
}
sort(records.begin(), records.end(), recordCmp);
// 为每个客户记录订单
map<string, vector<Record>> customers;
for (int i=1; i<n; i++){
if (records[i-1].pairWith(records[i])){
customers[records[i].name].push_back(records[i-1]);
customers[records[i].name].push_back(records[i]);
}
}
// 输出结果
for(auto customer=customers.begin(); customer!=customers.end(); customer++){
cout << customer->first;
records = customer->second;
printf(" %02d\n", records[0].month);
double totalCost = 0, cost;
for (int i=1; i<records.size(); i+=2){
cost = records[i].calcCostFromZero() - records[i-1].calcCostFromZero();
totalCost += cost;
records[i-1].print();
records[i].print();
printf("%d ", records[i].time - records[i-1].time);
printf("$%.02f\n", cost);
}
printf("Total amount: $%.02f\n", totalCost);
}
return 0;
}
Github(github.com):@chouxianyu
Github Pages(github.io):@臭咸鱼
知乎(zhihu.com):@臭咸鱼
博客园(cnblogs.com):@臭咸鱼
B站(bilibili.com):@绝版臭咸鱼
微信公众号:@臭咸鱼的快乐生活
转载请注明出处,欢迎讨论和交流!
原文:https://www.cnblogs.com/chouxianyu/p/14361390.html