A long-distance telephone company charges its customers by the following rules:
Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records.
Input Specification:
Each input file contains one test case. Each case has two parts: the rate structure, and the phone call records.
The rate structure consists of a line with 24 non-negative integers denoting the toll (cents/minute) from 00:00 - 01:00, the toll from 01:00 - 02:00, and so on for each hour in the day.
The next line contains a positive number
N
(
≤
1000
)
N (≤1000)
N(≤1000), followed by
N
N
N lines of records. Each phone call record consists of the name of the customer (string of up to 20 characters without space), the time and date (MM:dd:HH:mm
), and the word on-line
or off-line
.
For each test case, all dates will be within a single month. Each on-line
record is paired with the chronologically next record for the same customer provided it is an off-line
record. Any on-line
records that are not paired with an off-line
record are ignored, as are off-line
records not paired with an on-line
record. It is guaranteed that at least one call is well paired in the input. You may assume that no two records for the same customer have the same time. Times are recorded using a 24-hour clock.
Output Specification:
For each test case, you must print a phone bill for each customer.
Bills must be printed in alphabetical order of customers’ names. For each customer, first print in a line the name of the customer and the month of the bill in the format shown by the sample. Then for each time period of a call, print in one line the beginning and ending time and date (dd:HH:mm
), the lasting time (in minute) and the charge of the call. The calls must be listed in chronological order. Finally, print the total charge for the month in the format shown by the sample.
Sample Input:
10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
10
CYLL 01:01:06:01 on-line
CYLL 01:28:16:05 off-line
CYJJ 01:01:07:00 off-line
CYLL 01:01:08:03 off-line
CYJJ 01:01:05:59 on-line
aaa 01:01:01:03 on-line
aaa 01:02:00:01 on-line
CYLL 01:28:15:41 on-line
aaa 01:05:02:24 on-line
aaa 01:04:23:59 off-line
Sample Output:
CYJJ 01
01:05:59 01:07:00 61 $12.10
Total amount: $12.10
CYLL 01
01:06:01 01:08:03 122 $24.40
28:15:41 28:16:05 24 $3.85
Total amount: $28.25
aaa 01
02:00:01 04:23:59 4318 $638.80
Total amount: $638.80
Caution:
个人感觉这道题最难的地方在于理解题意,题目的意思是输入某个月内一些用户的电话记录(但这些记录不一定是按照时间顺序的)然后计算每个用户这个月的通话时间(分钟),并且按照输入的一天之中每个小时的费率计算出每个用户这个月的话费。
另外,通话时间是指一个“配对成功”的通话记录之间的时间,具体来说,每个通话记录都会有一个on-line
或off-line
的标志,每一个on-line
配对一个时间顺序上位于online
后面的同一个顾客的offline
,emmm这样说可能还是不太好理解,但是可以隐约感觉到需要对同一个顾客的通话记录按时间顺序做一个排序,然后我们可以输出一下每个用户的记录,在对比一下示例输出就好理解了。
另外一个需要注意的点是如果一个用户没有合法的通话记录的话,这个用户不能输出(而不是输出“费用为0”);
下面的注释部分结合了我的思路以及检查的顺序,帮助理解。
做好一部分检查一部分还是挺有用的,可以避免一些后期比较难看出来的错误。代码写完后第一遍提交PA,然后发现一个很蠢的地方错了(在计算费率的时候,用到了所有费率之和,我直接用了题目中的数据带入355),改了后就AC了。
Solution:
// Talk is cheap, show me the code
// Created by Pengxiao Guo 2021-08-12 12:45:53
// All rights reserved.
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
struct Customer{
string name;
vector<string> records;
float bill;
// records 里面的string有固定的格式:
// 0、1: 月份
// 2: :
// 3、4: 日期
// 5: :
// 6、7: 小时
// 8: :
// 9、10: 分钟
// 11: 空格
//
// 剩下的就是状态了,如果一共有 19 位那就是 on-line,如果是 20 位那就是 off-line
Customer(string n = ""){
name = n;
records.clear();
bill = 0;
}
~Customer(){}
};
bool cmp(Customer a, Customer b){
return a.name < b.name;
}
int main(){
vector<int> rate(24);
int n;
string month = "";
int totalRate = 0;
for(int i = 0; i < 24; ++i){
scanf("%d", &rate[i]);
totalRate += rate[i];
}
scanf("%d", &n);
vector<Customer> customers;
unordered_map<string, int> pos;
for(int i = 0; i < n; ++i){
string name = "", time = "", state = "";
cin >> name >> time >> state;
if(month == "") month = time.substr(0, 2);
if(pos.find(name) == pos.end()){
// 表示还没有这个顾客的记录,先将这位顾客加入到customers队列中,然后name的pos应该是customers.size() - 1;
customers.emplace_back(name);
pos[name] = customers.size() - 1;
customers[pos[name]].records.push_back(time + " " + state);
}
else customers[pos[name]].records.push_back(time + " " + state);
}
// // 到这里检查一下
// cout << endl;
// for(int i = 0; i < customers.size(); ++i){
// cout << "name: " << customers[i].name << endl;
// cout << "records: " << endl;
// for(int j = 0; j < customers[i].records.size(); ++j) cout << customers[i].records[j] << endl;
// cout << endl;
// }
// // 检查完毕,没问题
// 对 records 进行排序
for(int i = 0; i < customers.size(); ++i) sort(customers[i].records.begin(), customers[i].records.end());
// // 到这里检查一下
// cout << endl;
// for(int i = 0; i < customers.size(); ++i){
// cout << "name: " << customers[i].name << endl;
// cout << "records: " << endl;
// for(int j = 0; j < customers[i].records.size(); ++j) cout << customers[i].records[j] << endl;
// cout << endl;
// }
// // 检查完毕,没问题
// 对用户按姓名进行排序
sort(customers.begin(), customers.end(), cmp);
// // 然后更新 pos 数组
// for(int i = 0; i < customers.size(); ++i) pos[customers[i].name] = i;
// // 到这里检查一下
// cout << endl;
// for(int i = 0; i < customers.size(); ++i){
// cout << "name: " << customers[i].name << endl;
// cout << "records: " << endl;
// for(int j = 0; j < customers[i].records.size(); ++j) cout << customers[i].records[j] << endl;
// cout << endl;
// }
// cout << endl;
// // 检查完毕,没问题
// 现在边遍历边输出
for(int i = 0; i < customers.size(); ++i){
int paired = 0;
bool lastOn = false;
// lastOn == false 表示期望下一个遇到的是 on
// lastOn == true 表示期望下一个遇到的是 off
float totalBill = 0, bill = 0;
string startTime = "", endTime = "";
int sDay, sHour, sMinute, eDay, eHour, eMinute;
int minute = 0;
for(int j = 0; j < customers[i].records.size(); ++j){
if(customers[i].records[j].length() == 19){
// 表明当前的是 on
if(lastOn == false){
// 表明期望遇到的也是 on
startTime = customers[i].records[j].substr(3, 8);
// 期望遇到 off
lastOn = true;
}
else{
// 表明期望遇到的是 off
// 更新startTime
startTime = customers[i].records[j].substr(3, 8);
}
}
else{
// 表明当前的是 off
if(lastOn == true){
// 表明期望遇到的也是 off
endTime = customers[i].records[j].substr(3, 8);
sDay = (startTime[0] - '0') * 10 + (startTime[1] - '0');
sHour = (startTime[3] - '0') * 10 + (startTime[4] - '0');
sMinute = (startTime[6] - '0') * 10 + (startTime[7] - '0');
eDay = (endTime[0] - '0') * 10 + (endTime[1] - '0');
eHour = (endTime[3] - '0') * 10 + (endTime[4] - '0');
eMinute = (endTime[6] - '0') * 10 + (endTime[7] - '0');
// 计算 minute 和 bill
// startTime 的格式:dd-HH-mm
bill = 0;
minute = 0;
if(sDay == eDay && sHour == eHour){
bill = (eMinute - sMinute) * rate[sHour];
minute = eMinute - sMinute;
}
else if(sDay == eDay){
// 同一天 不同小时
bill = (60 - sMinute) * rate[sHour] + eMinute * rate[eHour];
minute = eMinute + 60 - sMinute;
for(int k = sHour + 1; k < eHour; ++k){
bill += 60 * rate[k];
minute += 60;
}
}
else{
// 不同天
// 先算出中间夹的那些天的 bill
bill += totalRate * 60 * (eDay - sDay - 1);
minute += 1440 * (eDay - sDay - 1);
// 再算出第一天的 bill
bill += (60 - sMinute) * rate[sHour];
minute += 60 - sMinute;
for(int k = sHour + 1; k < 24; ++k){
bill += 60 * rate[k];
minute += 60;
}
// 最后一天的 bill
bill += eMinute * rate[eHour];
minute += eMinute;
for(int k = 0; k < eHour; ++k){
bill += 60 * rate[k];
minute += 60;
}
}
if(paired == 0) printf("%s %s\n",customers[i].name.c_str(), month.c_str());
printf("%s %s %d $%.2f\n", startTime.c_str(), endTime.c_str(), minute, bill / 100);
totalBill += bill;
paired++;
lastOn = false;
}
else{
// 表明期望遇到的是 on
// 应该不用做什么,直接跳过
}
}
}
if(paired != 0) printf("Total amount: $%.2f\n", totalBill / 100);
// printf("Total amount: $%.2f\n", totalBill / 100);
}
return 0;
}
/*
首先要用什么来存储用户的数据,可以考虑用struct
struct Customer{
string name;
vector<string> records;
// records 里面的string有固定的格式:
// 0、1: 月份
// 2: :
// 3、4: 日期
// 5: :
// 6、7: 小时
// 8: :
// 9、10: 分钟
// 11: 空格
//
// 剩下的看状态了,如果一共有 19 位那就是 on-line,如果是 20 位那就是 off-line
Customer(){}
~Customer(){}
};
用什么来存储customers,我觉可以用 vector,这样好用 sort 排序
但是要做到随机访问的话还需要用到一个 unordered_map<string,int>;
vector<Customer> customers;
unordered_map<string, int> pos;
*/