PAT 1016. Phone Bills (25)
1016. Phone Bills (25)
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-lineSample 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
解析:题目中设置了一个Record的结构体,用来记录customer的记录,然后建立了一个Record的数组,并按照姓名和时间进行排序,使得同一客户的记录在数组中连续,并且内部按照时间的顺序排列。
关于题目中的匹配需要按照我们平时打电话的理解而不能按照表达式中的括号理解,比如说按照时间顺序为 on-line on-line off-line off-line,此时仅仅有中间的一对可以匹配,而两端的 匹配,即on-line对应的一定是后面第一个off-line。
关于题目中说一定保证有一对记录会配对指的是全部中会有一个配对,而并不是指每一个客户都会有一对记录会配对。因此输出前应该先判断时候有配对,否则不用输出任何东西。如果不考虑这个第 二,三个测试点会过不了。
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <sstream> 5 #include <iomanip> 6 #include <algorithm> 7 8 using namespace std; 9 10 struct Record 11 { 12 string month; 13 string name; 14 string time; 15 string type; 16 }; 17 18 int toll[24]; 19 Record records[1000]; 20 21 bool cmp(const Record& lhs, const Record& rhs) 22 { 23 if (lhs.name == rhs.name) 24 return lhs.time < rhs.time; 25 else 26 return lhs.name < rhs.name; 27 } 28 29 //calculate the duration of two time 30 int Duration(string start, string end) 31 { 32 if (start >= end) 33 return 0; 34 for (int i = 0; i < start.size(); i++) 35 if (start[i] == ':') 36 start[i] = end[i] = ' '; 37 int d1, d2, h1, h2, m1, m2; 38 stringstream ss; 39 ss << start; 40 ss >> d1 >> h1 >> m1; 41 ss.clear(); 42 ss << end; 43 ss >> d2 >> h2 >> m2; 44 45 return 24 * 60 * (d2 - d1) + 60 * (h2 - h1) + (m2 - m1); 46 } 47 48 //calculate the cost from "00:00:00" to the given time 49 double Cost(string time) 50 { 51 for (int i = 0; i < time.size(); i++) 52 if (time[i] == ':') 53 time[i] = ' '; 54 stringstream ss; 55 ss << time; 56 int d, h, m; 57 ss >> d >> h >> m; 58 double cost = 0; 59 int sum = 0; 60 for (int i = 0; i < 24; i++) 61 sum += toll[i]; 62 cost += (1.0 * sum / 100 * d *60); 63 for (int i = 0; i < h; i++) 64 cost += (1.0 * toll[i] / 100 * 60); 65 cost += (1.0 * toll[h] / 100 * m); 66 67 return cost; 68 } 69 70 int main() 71 { 72 for (int i = 0; i < 24; i++) 73 cin >> toll[i]; 74 int recordNum; 75 string month; 76 cin >> recordNum; 77 for (int i = 0; i < recordNum; i++) 78 { 79 string time; 80 cin >> records[i].name >> time >> records[i].type; 81 records[i].month = time.substr(0, 2); 82 records[i].time = time.substr(3, time.size() - 3); 83 } 84 sort(records, records + recordNum, cmp); 85 86 string name; 87 vector<Record> vec; 88 for (int i = 0; i < recordNum; i++) 89 { 90 //the same customer and store them in the vector 91 if (name.empty() || records[i].name == name) 92 { 93 vec.push_back(records[i]); 94 name = records[i].name; 95 } 96 //an another different customer or the last record 97 if ((!name.empty()&&records[i].name != name) || (i==recordNum-1)) 98 { 99 //check if there is any record paired 100 bool flag = false; 101 int j; 102 for (j = 0; j < vec.size(); j++) 103 { 104 if (vec[j].type == "on-line") 105 flag = true; 106 if (flag&& vec[j].type == "off-line") 107 break; 108 } 109 if (j == vec.size()) //no records paired and not print anything 110 { 111 name = records[i].name; 112 vec.clear(); 113 vec.push_back(records[i]); 114 continue; 115 } 116 //print the records 117 cout << name << " " << records[i-1].month << endl; 118 string start, end; 119 double totalCost = 0; 120 for (int i = 0; i < vec.size(); i++) 121 { 122 if (vec[i].type == "on-line") 123 start = vec[i].time; 124 else if (vec[i].type == "off-line"&&!start.empty()) 125 { 126 end = vec[i].time; 127 int total; 128 double cost; 129 total = Duration("00:00:00", end) - Duration("00:00:00", start); 130 cost = Cost(end) - Cost(start); 131 cout << start << " "<< end << " " << total << " $" << fixed << setprecision(2) << cost << endl; 132 totalCost += cost; 133 start.clear(); 134 } 135 } 136 cout << "Total amount: $" << totalCost << endl; 137 138 name = records[i].name; 139 vec.clear(); 140 vec.push_back(records[i]); 141 } 142 } 143 }