int tolls[24];
int N;
struct Record
{
string name, time, type;
};
// 计算分钟数和费用
double get_toll(string on_time, string off_time, int &cost_time)
{
int on_day, on_hour, on_min, off_day, off_hour, off_min;
on_day = stoi(on_time.substr(3, 2));
on_hour = stoi(on_time.substr(6, 2));
on_min = stoi(on_time.substr(9, 2));
off_day = stoi(off_time.substr(3, 2));
off_hour = stoi(off_time.substr(6, 2));
off_min = stoi(off_time.substr(9, 2));
double toll = 0;
while (on_day != off_day || on_hour != off_hour || on_min != off_min) {
toll += tolls[on_hour];
cost_time++;
on_min++;
if (on_min == 60) {
on_min = 0;
on_hour++;
}
if (on_hour == 24) {
on_hour = 0;
on_day++;
}
}
return toll / 100.0;
}
int main()
{
for(int i = 0; i < 24; ++i)
cin >> tolls[i];
cin >> N;
string month;
// map存储各用户的通话记录,名字按照字母序
map<string, vector<Record>> calls;
for(int i = 0; i < N; ++i)
{
Record record;
cin >> record.name >> record.time >> record.type;
month = record.time.substr(0, 2);
calls[record.name].push_back(record);
}
// 处理每个用户的通话记录:匹配 + 计算
for(auto &user : calls)
{
string name = user.first;
vector<Record> &logs = user.second;
double sum_toll = 0;
// 该用户通话记录按时间排序
sort(logs.begin(), logs.end(), [](const Record &r1,
const Record &r2){
return r1.time < r2.time;
});
// 匹配有效的通话记录
vector<pair<string, string>> lines;
for(size_t i = 0; i < logs.size() - 1; ++i)
{
if(logs[i].type == "on-line" && logs[i + 1].type == "off-line")
lines.push_back({logs[i].time, logs[i + 1].time});
}
// 无有效通话记录
if(lines.empty())
continue;
// 可能某个用户没有有效的通话记录,这行打印信息不能放在前面
cout << name << " " << month << endl;
// 计算分钟数和费用并打印信息
for(auto [on_time, off_time] : lines)
{
int cost_time = 0;
double toll = get_toll(on_time, off_time, cost_time);
cout << on_time.substr(3) << " " << off_time.substr(3) << " " << cost_time;
printf(" $%.2f\n", toll);
sum_toll += toll;
}
printf("Total amount: $%.2f\n", sum_toll);
}
return 0;
}
PTA 1016 Phone Bills
于 2025-02-09 15:30:49 首次发布
1100

被折叠的 条评论
为什么被折叠?



