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
)
{
double total_bills
= mins
* cost
[hour
] + day
* cost
[24] * 60;
for (int i
= 0; i
< hour
; i
++)
{
total_bills
+= cost
[i
] * 60;
}
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
{
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
)
{
double total_bills
= mins
* cost
[hour
] + day
* cost
[24] * 60;
for (int i
= 0; i
< hour
; i
++)
{
total_bills
+= cost
[i
] * 60;
}
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)
{
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
++)
{
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
;
}
}
}
return 0;
}