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 (≤), 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

思路:

  • 将各条记录存入结构体数组中,将记录按照先姓名后时间的顺序排序,其中time记录了从00:00:00到当前时间dd:hh:mm的总分钟数;
  • 筛选数据的时候只有前一条记录状态是on,紧邻的后一条记录状态是off的时候才是合格数据;
  • 计算话费的函数中计算的结果是从00:00:00到当前时间dd:hh:mm的总话费,得到一组的话费只要计算一组中两条记录之间话费的差值即可(注意题目给出单价是cents/minute,输出金额是美元);
  • 因为一个用户对应数条记录,所以使用map映射,map<string,vector<node>>表示用户名到记录的映射,逐个用户的逐条合格记录输出即可。
  • 注意:因为数组rate没有初始化导致最后一个不通过

参考柳神的思路,因为这道题我一个月没做题。。。我太菜了我忏悔 o(TωT)o 

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <map>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 //存储记录
 9 struct node
10 {
11     string name;
12     int state, time, mouth, dd, hh, mm;
13 };
14 
15 //从00:00:00计算话费
16 double Bill(node call, int rate[]) {
17     double sum = rate[call.hh] * call.mm + call.dd*60*rate[24];
18     for (int i = 0; i < call.hh; i++) {
19         sum += rate[i] * 60;
20     }
21     return sum / 100.0;
22 }
23 
24 bool cmp(node a, node b) {
25     if (a.name == b.name) {
26         return a.time < b.time;
27     }
28     else
29         return a.name < b.name;
30 }
31 
32 int main() {
33     int rate[25] = {0};
34     for (int i = 0; i < 24; i++) {
35         cin >> rate[i];
36         rate[24] += rate[i];//用于计算一天的话费
37     }
38     int n;
39     cin >> n;
40     vector<node>v(n);
41     for (int i = 0; i < n; i++) {
42         cin >> v[i].name;
43         scanf("%d:%d:%d:%d", &v[i].mouth, &v[i].dd, &v[i].hh, &v[i].mm);
44         v[i].time = v[i].dd * 24 * 60 + v[i].hh * 60 + v[i].mm;//总分钟数
45         string tmpstate;
46         cin >> tmpstate;
47         v[i].state = (tmpstate == "on-line") ? 1 : 0;
48     }
49     sort(v.begin(), v.end(), cmp);
50     map<string, vector<node>>customer;
51     for (int i = 1; i < n; i++) {
52         if (v[i].name == v[i - 1].name&&v[i].state == 0 && v[i - 1].state == 1) {
53             customer[v[i].name].push_back(v[i - 1]);
54             customer[v[i].name].push_back(v[i]);
55         }
56     }
57     //通过迭代器进行访问
58     for (auto it : customer) {
59         cout << it.first;
60         vector<node>tmpv = it.second;
61         double total = 0.0;
62         printf(" %02d\n", tmpv[0].mouth);//同一个用户的记录中月份都是一样的
63         for (int i = 1; i < tmpv.size(); i += 2) {
64             printf("%02d:%02d:%02d %02d:%02d:%02d", tmpv[i - 1].dd, tmpv[i - 1].hh, tmpv[i - 1].mm, tmpv[i].dd, tmpv[i].hh, tmpv[i].mm);
65             cout << " "<<tmpv[i].time - tmpv[i - 1].time;
66             double tmpto = Bill(tmpv[i], rate) - Bill(tmpv[i - 1], rate);
67             printf(" $%.2lf\n",tmpto);
68             total += tmpto;
69         }
70         printf("Total amount: $%.2lf\n", total);
71     }
72     return 0;
73 }

猜你喜欢

转载自www.cnblogs.com/PennyXia/p/12588408.html
今日推荐