题意
每个小时打电话所需费用都不相同,给出若干客户的若干记录,每一条是开始打电话或挂断电话的时间,输出每个客户每次打电话的时长和开销以及所有电话记录的总开销
思路
总结一下别人代码的思路:
- 巧妙使用
sort()
函数,就可以让相同名字的顾客记录凑到一起并且按时间先后排列,然后再遍历这堆顾客记录把合法的再挑出来即可 - 使用
map<string,vector<node> >
来存合法记录,这样就可以对每个顾客的记录进行依次遍历 - 关于cost的计算,巧妙使用两个时间点的从零开始的价格相减来计算实际打电话花费的金额
AC代码
#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;
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];
}
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 * 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 - 1].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) {
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;
}