PAT 1016. Phone Bills (25)
题目分析
原题相信大家都看见了,题目几乎不涉及算法,主要就是考大家的细心,题目大意就是给我们所有用户的长途电话记录,根据用户分组,计算每个用户需要交的电话费,打印账单,这个题隐含着如果一个用户没有一次完整的打电话记录就不输出,我起初在这个上面栽了跟头,后来及时更正了。
解题逻辑
- 读入花费的收费信息
- 读入用户的信息,使用
map
将不同的用户信息分隔开,将on-line
和off-line
记录放到用户的记录表中,标记记录类型。 - 对
map
进行遍历,对用户的on-line
、off-line
记录排序,找出所有可以组成完整通话的记录,计算费用并记录。 - 输出信息
代码
#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int rate[24];
struct Record {
int day, hour, min;
char type;
Record(int day, int hour, int min, char ch) :day(day), hour(hour), min(min), type(ch) {}
// 便于排序
friend bool operator<(const Record &a, const Record &b) {
return a.day * 1440 + a.hour * 60 + a.min < b.day * 1440 + b.hour * 60 + b.min;
}
// 便于输出分钟差
friend int operator-(const Record &a, const Record &b) {
return a.day * 1440 + a.hour * 60 + a.min - b.day * 1440 - b.hour * 60 - b.min;
}
};
struct Bill {
int start;
int end;
double cost;
};
struct User {
string name;
vector<Record> recs;
friend bool operator<(const User &a, const User &b) {
return a.name < b.name;
}
};
double calcu_cost(const Record &start, const Record &end) {
// 判断是否跨天了
double cost = 0;
if (start.day == end.day) {
// 没有跨天
int hour = start.hour;
// 开始没有考虑时间在同一个小时,被坑好大会儿
if (start.hour == end.hour) {
cost += rate[hour] * (end.min - start.min);
}
else {
cost += rate[hour] * (60 - start.min);
++hour;
while (hour < end.hour) {
cost += rate[hour] * 60;
hour++;
}
cost += rate[end.hour] * end.min;
}
}
else {
// 跨天了,问题是有可能跨好几天。。。
int cross_days = end.day - start.day - 1;
for (int i = 0; i < 24; ++i) {
cost += rate[i] * 60 * cross_days;
}
// 计算只跨一次00:00的费用
int hour = start.hour;
int end_hour = end.hour + 24;
cost += rate[hour] * (60 - start.min);
hour++;
while (hour < end_hour) {
cost += rate[hour % 24] * 60;
++hour;
}
cost += rate[end.hour] * end.min;
}
return cost / 100;
}
int main() {
for (int i = 0; i < 24; ++i) {
scanf("%d", &rate[i]);
}
int cnt;
scanf("%d", &cnt);
char name[30];
char on_off[20];
string s;
int month, day, hour, min;
map<string, User>m;
while (cnt--) {
scanf("%s %d:%d:%d:%d %s", name, &month, &day, &hour, &min, on_off);
s = name;
m[s].recs.emplace_back(day, hour, min, on_off[1]);
}
// 开始算帐单,一个一个用户的输出
for (auto &u : m) {
vector<Bill> bills;
Bill b;
bool good = false;
double total = 0;
double once = 0;
auto &rs = u.second.recs;
// 把on和off先排序
sort(rs.begin(), rs.end());
int size = rs.size();
for (int i = 0; i < size - 1; ++i) {
if (rs[i].type == 'n' && rs[i + 1].type == 'f') {
once = calcu_cost(rs[i], rs[i + 1]);
b.start = i;
b.end = i + 1;
b.cost = once;
total += once;
bills.push_back(b);
good = true;
}
}
if (good) {
printf("%s %02d\n", u.first.c_str(), month);
int start, end;
double cost;
for (auto &bill : bills) {
start = bill.start;
end = bill.end;
cost = bill.cost;
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", rs[start].day, rs[start].hour,
rs[start].min, rs[end].day, rs[end].hour, rs[end].min, rs[end] - rs[start],
cost);
}
printf("Total amount: $%.2f\n", total);
}
}
return 0;
}
注意事项
计算花费很容易出错,记得细心。