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), followed by 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
题意分析
这题的输入是先给出24小时每个小时的话费每分钟话费,然后给出n,随后附上n条通话记录,每条通话记录内容有顾客姓名,记录时间,记录类型,记录类型有上线记录和下线记录。
题目的输出要求给出每个顾客的有效通话记录,每条通话记录的开始和结束时间,每条通话记录的单独话费和最后的总花费。
个人思路
这题其实还是对排序算法的应用,有几个关键点如下所示。
1、要对通话记录进行排序,要实现先按字母序排序,后按通话时间从小到大排序,因此要自己写出一个比较函数。
2、要计算出每条通话记录从上线到下线的时间段内的总话费,每个小时的每分钟话费可能都不同。我是在一个循环中模拟时间的增长,每个小时单独计算增加的话费,同时将开始时间增加,直到两个时间相等时退出循环。
3、要判断哪些通话记录是有效的,要注意:如果上条记录是上线的,下条记录如果是上线则替换上条记录的上线时间,下条记录如果是下线的则进行话费计算;如果上条记录是下线的,下条记录如果是下线的则直接忽略,下条记录如果是上线的则开始新的一条记录。
4、本题最大的坑:输出时只输出话费大于0的顾客,如果话费为0,连名字都不输出。
代码实现
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
#define ll long long
#define eps 1e-8
#define INF 0x7FFFFFFF
using namespace std;
// 24小时话费
int cents_per_min[24] = {0};
// 时间结构体
struct CallTime {
int month, day, hour, minute;
};
// 通话记录结构体
struct CallRecord {
string name;
CallTime call_time;
bool online; // 记录类型 1上线 0下线
};
bool cmp(CallRecord r1, CallRecord r2) {
if (r1.name == r2.name) {
// return r1.call_time < r2.call_time
if (r1.call_time.day < r2.call_time.day) return true;
else if(r1.call_time.day > r2.call_time.day)return false;
if (r1.call_time.hour < r2.call_time.hour) return true;
else if(r1.call_time.hour > r2.call_time.hour) return false;
if (r1.call_time.minute < r2.call_time.minute) return true;
else if(r1.call_time.minute > r2.call_time.minute)return false;
}
return r1.name < r2.name;
}
// 求出两个时间的分钟差
int time_sub(CallTime t1, CallTime t2) {
int min1, min2;
min1 = t1.day*24*60 + t1.hour*60 + t1.minute;
min2 = t2.day*24*60 + t2.hour*60 + t2.minute;
return abs(min1-min2);
}
// 求出某个电话记录的话费
int phone_cost(CallTime t1, CallTime t2) {
int ret = 0;
while (!(t1.day == t2.day && t1.hour == t2.hour && t1.minute == t2.minute)) {
if (t1.day == t2.day && t1.hour == t2.hour) {
ret += cents_per_min[t1.hour]*(t2.minute-t1.minute);
t1.minute = t2.minute;
}
else {
ret += cents_per_min[t1.hour]*(60-t1.minute);
t1.minute = 0;
if (t1.hour == 23) {
t1.hour = 0;
t1.day ++;
}
else {
t1.hour ++;
}
}
}
return ret;
}
int main() {
// 输入cents per minute
for (int i = 0; i < 24; i ++) {
cin >> cents_per_min[i];
}
// 输入记录个数
int n;
cin >> n;
// 对每条记录进行存储
vector<CallRecord> records;
for (int i = 0; i < n; i ++) {
// 将输入信息先存放到record里
CallRecord record;
string name, call_time, type;
cin >> name >> call_time >> type;
record.name = name;
const char *s = call_time.data();
sscanf(s, "%d:%d:%d:%d", &record.call_time.month, &record.call_time.day, &record.call_time.hour, &record.call_time.minute);
if (type == "on-line") record.online = true;
else record.online = false;
// 将记录存入vector
records.push_back(record);
}
// 按照先字母表,后时间前后进行排序
sort(records.begin(), records.end(), cmp);
int sum = 0; // 记录总开销
bool last_online = false; // 记录上一条记录是否是online
CallTime begin_time, end_time; // 一条通话记录的开始和结束时间
map <string, int> idx; // 建立顾客姓名和姓名编号的映射
int name_cnt = 0; // 顾客姓名数量
for (int i = 0; i < n; i ++) {
CallRecord record = records[i];
// 如果是上线记录,则记录开始电话的时间
if (record.online) {
begin_time = record.call_time;
last_online = true;
}
// 如果是下线记录,且上条记录是上线记录
else if (last_online && !record.online) {
end_time = record.call_time;
last_online = false;
// 计算分钟数和花费数
int minutes = time_sub(begin_time, end_time);
int rec_cost = phone_cost(begin_time, end_time);
sum += rec_cost;
// 第一次时打印名字
if(idx[record.name] == 0) {
idx[record.name] = ++name_cnt;
cout << record.name << " ";
printf("%02d\n", record.call_time.month);
}
// 打印通话记录
printf("%02d:%02d:%02d ", begin_time.day, begin_time.hour, begin_time.minute);
printf("%02d:%02d:%02d ",end_time.day, end_time.hour, end_time.minute);
printf("%d $%.2lf\n", minutes, 1.0*rec_cost/100);
}
bool customer_end = false;
if (i == n-1) customer_end = true;
else if (record.name != records[i+1].name) customer_end = true;
if (customer_end) {
if (sum != 0) printf("Total amount: $%.2lf\n", 1.0*sum/100);
sum = 0;
last_online = false;
}
}
return 0;
}
总结
学习不息,继续加油
PAT奇奇怪怪的坑实在是太多了,审题真的很重要。