1493. 电话账单
题目描述
长途电话公司按以下规则向客户收费:
拨打长途电话每分钟要花费一定的费用,具体收费取决于拨打电话的时间。
客户开始拨打长途电话的时间将被记录,客户挂断电话的时间也将被记录。
每个月都要给客户发送一次话费账单,账单中应包含每次通话记录以及相关收费等信息。
给定一组电话记录,你的工作是为客户准备帐单。
输入格式
输入包含两部分:费率结构和电话记录。
费率结构由一行组成,该行包含24个非负整数,分别表示从 00:00-01:00 的收费(分/分钟),从 01:00-02:00 的收费,以此类推…
下一行包含一个正整数 NN。
接下来 NN 行,每行包含一条记录。
每个记录由客户名称(最多 2020 个字符的字符串,不带空格),时间和日期(mm:dd:hh:mm)以及单词 on-line 或 off-line 组成。
所有日期都在同一个月内,每个 on-line 记录都与按时间顺序排列的同一位客户的下一条记录配对,但前提是这条记录是 off-line。
所有未与 off-line 记录配对的 on-line 记录以及未与 on-line 记录配对的 off-line 记录都必须忽略。
输入中至少包含一个成功的配对。
同一客户在同一时间不会有两个或以上的电话记录。
使用 2424 小时制记录时间。
输出格式
你需要为每个客户打印电话费。
账单必须按照客户姓名的字母顺序(按ASCII码顺序,大写字母在前,小写字母在后)打印。
对于每个客户,首先以示例显示的格式在一行中打印客户名称和帐单月份。
然后,对于每个通话时间段,在一行中分别打印开始和结束时间和日期(dd:hh:mm),持续时间(以分钟为单位)和通话费用。
通话必须按时间顺序列出。
最后,以示例显示的格式打印该月的总费用。
注意,没有任何有效通话记录的客户直接忽略,不予打印账单。
样例
数据范围
1≤N≤10001≤N≤1000
输入样例:
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
输出样例:
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
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
struct node {
string name;
int status, month, time, day, hour, minute;
};
bool cmp(node a, node b) {
return a.name != b.name ? a.name < b.name : a.time < b.time; //先按照名字排序
}
double billFromZero(node call, int *rate) {//本题计算花费比较麻烦
double total = rate[call.hour] * call.minute + rate[24] * 60 * (call.day-1);
for (int i = 0; i < call.hour; i++)
total += rate[i] * 60;
return total / 100.0;
}
int main() {
int rate[25] = {0}, n;
for (int i = 0; i < 24; i++) {
scanf("%d", &rate[i]);
rate[24] += rate[i]; //rate[24]*60就是一天的花费
}
scanf("%d", &n);
vector<node> data(n);
for (int i = 0; i < n; i++) {
cin >> data[i].name;
scanf("%d:%d:%d:%d", &data[i].month, &data[i].day, &data[i].hour, &data[i].minute);
string temp;
cin >> temp;
data[i].status = (temp == "on-line") ? 1 : 0;
data[i].time = (data[i].day-1) * 24 * 60 + data[i].hour * 60 + data[i].minute;
}
sort(data.begin(), data.end(), cmp);
map<string, vector<node> > custom;//已经按照名字字典序排好
for (int i = 1; i < n; i++) {
if (data[i].name == data[i - 1].name && data[i - 1].status == 1 && data[i].status == 0) {
custom[data[i].name].push_back(data[i - 1]);
custom[data[i].name].push_back(data[i]);
}
}
for (auto it : custom) {
vector<node> temp = it.second;
cout << it.first;
printf(" %02d\n", temp[0].month);
double total = 0.0;
for (int i = 1; i < temp.size(); i += 2) { //temp里面实体对一对的
double t = billFromZero(temp[i], rate) - billFromZero(temp[i - 1], rate);
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", temp[i - 1].day, temp[i - 1].hour, temp[i - 1].minute, temp[i].day, temp[i].hour, temp[i].minute, temp[i].time - temp[i - 1].time, t);
total += t;
}
printf("Total amount: $%.2f\n", total);
}
return 0;
}