A1016. Phone Bills

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

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<string.h>
 5 using namespace std;
 6 typedef struct{
 7     char id[21];
 8     int m, dd, hh, mm;
 9     int on_line;
10 }info;
11 int N, rates[24];
12 bool cmp(info a, info b){
13     if(strcmp(a.id, b.id) < 0)
14         return true;
15     else if(strcmp(a.id, b.id) == 0 && a.m < b.m)
16         return true;
17     else if(strcmp(a.id, b.id) == 0 && a.m == b.m && a.dd < b.dd)
18         return true;
19     else if(strcmp(a.id, b.id) == 0 && a.m == b.m && a.dd == b.dd && a.hh < b.hh)
20         return true;
21     else if(strcmp(a.id, b.id) == 0 && a.m == b.m && a.dd == b.dd && a.hh == b.hh && a.mm < b.mm)
22         return true;
23     else return false;
24 }
25 double counts(info a, info b, int &length){
26     double sum = 0;
27     length = 0;
28     while(a.dd < b.dd || a.hh < b.hh || a.mm < b.mm){
29         sum += rates[a.hh];
30         a.mm++;
31         length++;
32         if(a.mm == 60){
33             a.mm = 0;
34             a.hh++;
35         }
36         if(a.hh == 24){
37             a.hh = 0;
38             a.dd++;
39         }
40     }
41     return sum;
42 }
43 int main(){
44     info records[1001];
45     char id[21] = {'\0'}, states[10];
46     for(int i = 0; i < 24; i++)
47         scanf("%d", &rates[i]);
48     scanf("%d", &N);
49     for(int i = 0; i < N; i++){
50         scanf("%s %d:%d:%d:%d %s", records[i].id, &records[i].m, &records[i].dd, &records[i].hh, &records[i].mm, states);
51         if(strcmp(states, "on-line") == 0)
52             records[i].on_line = 1;
53         else
54             records[i].on_line = 0;
55     }
56     sort(records, records + N, cmp);
57     int prt = 0, length;
58     double charge = 0, temp;
59     for(int i = 0; i < N - 1; i++){
60         if(strcmp(records[i].id, records[i + 1].id) == 0 && records[i].on_line == 1 && records[i + 1].on_line == 0){
61             if(prt == 0){
62                 printf("%s %02d\n", records[i].id, records[i].m);
63                 prt = 1;
64             }
65             temp = counts(records[i], records[i + 1], length) / 100;
66             printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", records[i].dd, records[i].hh, records[i].mm, records[i + 1].dd, records[i + 1].hh, records[i + 1].mm, length, temp);
67             charge += temp;
68         }else if(strcmp(records[i].id, records[i + 1].id) != 0){
69             if(prt == 1){
70                 printf("Total amount: $%.2f\n", charge);
71             }
72             charge = 0;
73             prt = 0;
74         }
75     }
76     if(prt == 1){
77         printf("Total amount: $%.2f", charge);
78     }
79     cin >> N;
80     return 0;
81 }
View Code

总结:

1、本题的要求是,对同一个用户,先对无序的记录按照时间排序,只有两条紧邻的且前者为on-line后者为off-line的记录才视作有效记录,对这些有效记录作一些统计,并输出。

2、首先采用struct记录每个时间节点的信息。为了方便后续处理,对这些信息进行排序,相同的人的信息在一起,且同一个人的信息按时间顺序排列。如果是自己写排序算法,则可采用具有稳定性的排序算法进行两次排序,第一次按照时间顺序,第二次按照人名的字典序。如果采用c++自带sort函数,在自定义比较函数cmp时,a与b的大小定义为:如果a的id小于b的id,则a<b, id相等则比较时间,a的时间小于b的时间,则a<b。

3、在计算单次通话时长和通话费用时,可以采用之前的日期计算的套路,从小日期往大日期累加1,过程中不断注意进制。循环进行的条件为 a.dd < b.dd || a.hh < b.hh || a.mm < b.mm, 因为a时间大于等于b时间是 a.dd >= b.dd && a.hh >= b.hh && a.mm >= b.mm。

4、在做这道题的时候还遇到一个问题,在count函数加了一个引用传参时,编译器报错,百思不得其解。网上查了之后才知道std里有count函数模板,与我定义的count有冲突,改掉名字之后成功编译。

5、double变量,输入时为%lf, 输出时为%f。

posted @ 2018-01-22 20:39  ZHUQW  阅读(172)  评论(0编辑  收藏  举报