题目描述
1016 Phone Bills (25)(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-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
分析
这道题本身并不难,大概思路就是把通话记录先按名字排序,同一名字下再按时间排序。最恶心的地方是很麻烦,代码量挺长的,而且还必须考虑一些小细节
SourceCode
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iterator>
#include<iomanip>
using namespace std;
/*存储每条通话记录*/
struct Record {
string name;
string time;
int state;
};
/*仅作为全局变量使用*/
struct Info {
float cost;
int l_time;
}info;
bool compareName(Record r1, Record r2);
bool compareTime(Record r1, Record r2);
void recordSort(vector<Record>&recordlist);
void matchOutput(vector<Record>&recordlist, int *price);
void cost(string begin, string end, int *price);
void inputData(vector<Record>&recordlist, int *price, int* n);
void priceProcess(int *price);
int main() {
int price[24];
vector<Record>recordlist;
int n;
inputData(recordlist, price, &n);
priceProcess(price);
recordSort(recordlist);
matchOutput(recordlist, price);
}
bool compareName(Record r1, Record r2) {
if (r1.name < r2.name) {
return true;
}
return false;
}
bool compareTime(Record r1, Record r2) {
if (r1.time < r2.time) {
return true;
}
return false;
}
/*输入数据的函数*/
void inputData(vector<Record>&recordlist, int *price, int* n) {
int i;
Record record;
string state;
for (i = 0; i < 24; i++) {
cin >> price[i];
}
cin >> *n;
for (i = 0; i < *n; i++) {
cin >> record.name;
cin >> record.time;
cin >> state;
if (state == "on-line")record.state = 1;
else
record.state = 0;
recordlist.push_back(record);
}
}
void recordSort(vector<Record>&recordlist) {
int i;
sort(recordlist.begin(), recordlist.end(), compareName);
vector<Record>::iterator it = recordlist.begin();
int begin, end = 0;
for (i = 0; i < recordlist.size() - 1; i++) {
if (recordlist[i].name != recordlist[i + 1].name) {
begin = end;
end = i + 1;
sort(it + begin, it + end, compareTime);
}
}
begin = end;
end = i + 1;
sort(it + begin, it + end, compareTime);
}
/*结果若是on-line和off-line相配对的话则输出*/
void matchOutput(vector<Record>&recordlist, int *price) {
int i;
float total = 0;
bool flag = true;//标志位,保证一个配对都没有的人不会被输出
cout << setiosflags(ios::fixed) << setprecision(2);
for (i = 0; i < recordlist.size() - 1; i++) {
if (recordlist[i].name != recordlist[i + 1].name) {
flag = true;
}
else if (recordlist[i].state == 1 && recordlist[i + 1].state == 0) {
if (flag == true) {
if (total != 0)cout << "Total amount: $" << total << endl;
total = 0;
cout << recordlist[i + 1].name << " " << recordlist[i + 1].time[0] << recordlist[i + 1].time[1] << endl;
flag = false;
}
cost(recordlist[i].time, recordlist[i + 1].time, price);
cout << recordlist[i].time.substr(3,8) << " " << recordlist[i + 1].time.substr(3,8) << " " << info.l_time << " $" << info.cost / 100 << endl;
total += info.cost / 100;
}
}
if (total != 0)cout << "Total amount: $" << total << endl;
}
/*计算时间和花费
并存储在全局变量组成的结构体info中
*/
void cost(string begin, string end, int *price) {
int dd1, dd2, hh1, hh2, mm1, mm2, time;
float money = 0;
dd1 = (begin[3] - '0') * 10 + (begin[4] - '0');
dd2 = (end[3] - '0') * 10 + (end[4] - '0');
hh1 = (begin[6] - '0') * 10 + (begin[7] - '0');
hh2 = (end[6] - '0') * 10 + (end[7] - '0');
mm1 = (begin[9] - '0') * 10 + (begin[10] - '0');
mm2 = (end[9] - '0') * 10 + (end[10] - '0');
//时间相减可以换种思路变得很简单,比如time2-time1=(time2-time0)-(time1-time0)
time = (dd2 - dd1) * 24 * 60 + hh2 * 60 + mm2 - hh1 * 60 - mm1;
money += (dd2 - dd1)*price[23];
if (hh2 != 0) {
money += price[hh2 - 1];
money += (price[hh2] - price[hh2 - 1]) / 60 * mm2;
}
else {
money += price[hh2] / 60 * mm2;
}
if (hh1 != 0) {
money -= price[hh1 - 1];
money -= (price[hh1] - price[hh1 - 1]) / 60 * mm1;
}
else {
money -= price[hh1] / 60 * mm1;
}
info.cost = money;
info.l_time = time;
}
/*对price进行处理,处理之后的price[i]表示从0点开始到(i+1)点整的花费*/
void priceProcess(int *price) {
int i;
price[0] = price[0] * 60;
for (i = 1; i < 24; i++) {
price[i] = price[i] * 60 + price[i - 1];
}
}