PAT-1016 Phone Bills

    科技2024-04-01  78

    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

    思路

    用结构体customer存放每个用户的total_amount每个月的总bills,用二维结构体(month)数组存放用户每个月信息,二维数组的行为12个月,列为结构体month:包含time和status。

    对每个月的每个月按照time进行排序,然后判断是不是一个on-line跟着一个off-line,是的话输出;

    计算bills,可以将on-line的day:hour:mins - 00:00:00 ,并换算成对应的分钟,然后计算bills;同理,off-line也是,然后off-line的结果减去on-line的结果即是所得的bills

    double current_time_bills(int *cost, int day, int hour, int mins) { // 返回当前 day:hour:mins这个时间的bills double total_bills = mins * cost[hour] + day * cost[24] * 60; // cost[24]表示一天24小时的总bills for (int i = 0; i < hour; i++) { total_bills += cost[i] * 60; } // cout << "day: " << day << " hour: " << hour << " mins: " << mins << endl; // cout << "total_bills: " << total_bills << endl; return total_bills / 100; }

    需要注意用户存在有效数据才输出,此外,每个月份对应于一个Total amount(卡了好长时间)

    此外:对于二维结构数组的sort排序问题,如果向下面这么写,sort结果不变

    sort(temp.records[i].begin(), temp.records[i].end(),cmp); // 排序

    建议先用temp保存,然后对temp进行排序,然后在将temp重新赋值回去

    Code

    #include <iostream> #include <vector> #include <map> #include <algorithm> #include <iomanip> using namespace std; struct month // month要先定义在customer之前 { string time; string status; }; struct customer { double total_amount; vector<vector<month>> records; }; bool cmp(const month &a, const month &b) { if (a.time < b.time) return 1; return 0; } double current_time_bills(int *cost, int day, int hour, int mins) { // 返回当前 day:hour:mins这个时间的bills double total_bills = mins * cost[hour] + day * cost[24] * 60; // cost[24]表示一天24小时的总bills for (int i = 0; i < hour; i++) { total_bills += cost[i] * 60; } // cout << "day: " << day << " hour: " << hour << " mins: " << mins << endl; // cout << "total_bills: " << total_bills << endl; return total_bills / 100; } int main() { int cost[25] = {0}; for (int i = 0; i < 24; i++) { cin >> cost[i]; cost[24] += cost[i]; } int n; cin >> n; string name, day_time, status; map<string, customer> data; for (int i = 1; i <= n; i++) { cin >> name >> day_time >> status; customer temp; temp.records.resize(13); temp.total_amount = 0; if (data.count(name) == 0) data.insert(pair<string, customer>(name, temp)); int index = (day_time[0] - '0') * 10 + day_time[1] - '0'; month month_temp; month_temp.status = status; month_temp.time = day_time.substr(3); data[name].records[index].push_back(month_temp); } for (auto it = data.begin(); it != data.end(); it++) { customer temp = it->second; // 当前用户记录数组 string temp_name = it->first; // 当前用户名 for (int i = 1; i <= 12; i++) // 遍历每个用户的月份 { int tag = 0; // 设置每个月 用户名和月份 只输出一次 if (temp.records[i].size() != 0) { // cout << temp_name << " "; // 需要注意用户有有效数据输出 // // cout << temp.records[i].size() << endl; // printf("%02d\n", i); // 输出月份 vector<month> a = temp.records[i]; sort(a.begin(), a.end(), cmp); temp.records[i] = a; string online_time; string offline_time; for (int j = 1; j < temp.records[i].size(); j++) { // 需要判断用户的数据是有效的才输出,有效=> 先on-line,后off-line if (temp.records[i][j - 1].status == "on-line" && temp.records[i][j].status == "off-line") { if (tag == 0) { cout << temp_name << " "; // 用户名 printf("%02d\n", i); // 月份 tag = 1; } online_time = temp.records[i][j - 1].time; offline_time = temp.records[i][j].time; int day_diff = ((offline_time[0] - '0') * 10 + offline_time[1] - '0') - ((online_time[0] - '0') * 10 + online_time[1] - '0'); int hour_diff = ((offline_time[3] - '0') * 10 + offline_time[4] - '0') - ((online_time[3] - '0') * 10 + online_time[4] - '0'); int min_diff = ((offline_time[6] - '0') * 10 + offline_time[7] - '0') - ((online_time[6] - '0') * 10 + online_time[7] - '0'); int online_time_min = (online_time[6] - '0') * 10 + online_time[7] - '0'; int online_time_hour = (online_time[3] - '0') * 10 + online_time[4] - '0'; int online_time_day = (online_time[0] - '0') * 10 + online_time[1] - '0'; int offline_time_min = (offline_time[6] - '0') * 10 + offline_time[7] - '0'; int offline_time_hour = (offline_time[3] - '0') * 10 + offline_time[4] - '0'; int offline_time_day = (offline_time[0] - '0') * 10 + offline_time[1] - '0'; int one_line_total_diff = day_diff * 24 * 60 + hour_diff * 60 + min_diff; // 一次完整通话的分钟数 double one_line_total_bills = current_time_bills(cost, offline_time_day, offline_time_hour, offline_time_min) - current_time_bills(cost, online_time_day, online_time_hour, online_time_min); data[temp_name].total_amount += one_line_total_bills; cout << online_time << " " << offline_time << " " << one_line_total_diff << " " << "$" << fixed << setprecision(2) << one_line_total_bills << endl; } } if (tag == 1) cout << "Total amount: $" << data[temp_name].total_amount << endl; } // 二维vector数组,数组元素为结构体,如何对每一行按照结构体的字段进行排序 // sort(temp.records[i].begin(), temp.records[i].end(),cmp); // 排序 // int size = data[it->first].records[i].size(); // for (int j = 0; j<size; j++){ // cout << data[it->first].records[i][j].time << endl; // } } } return 0; } // 思路正确,但是就是输出控制错误,花费了太多时间, // 需要注意用户存在有效数据才输出,此外,每个月份对应于一个Total amount
    Processed: 0.010, SQL: 8