[PAT A1016]Phone Bills

[PAT A1016]Phone Bills

题目描述

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.

输入格式

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.

输出格式

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.

输入样例

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

输出样例

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. 题目大意:题目首先输入24个数,其中第i个数表示时间在[I-1,1]这个时间段每分钟的电话费,例如,第一个数是10,表示在[0,1]小时内,电话费是1毛钱每分钟。然后输入电话记录数,对于每个人的电话记录数按照时间排序,如果某一个电话记录是on-line然后下一个电话是off-line,这就说明他们构成一次完整的通话。如果某一个电话是on-line状态而他的下一个电话是on-line,这样就不构成一个完整的通话;
  2. 我们要做的就是按照姓名从低到高输出他们的账单,分别是每次通话的起始时间和终止时间,后面跟着的是此次通话的时长和此次通话的价格,然后我们需要统计他们这个月的电话账单,并输出他们这个月的总电话费
  3. 这个题目很复杂,我第一次做的时候总是有两组数据不能通过,后面我使用了map,才把逻辑关系理的更加清楚,这里要说的是我计算通话时间的算法,我觉得需要描述一下,帮助读者理解。我首先输入两个时间,然后用小的时间不断一分钟一分钟地加上去,然后每加一分钟就增加该时间段一分钟的价格,直到两个时间相等为止,那就是总的通话时间和通话费用。
  4. 具体代码和注释如下:
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
struct record
{
 string name, state; //姓名和状态(on-line或者off-line)
 int month, day, hour, minute;
};
int price[24]; //记录每个时间段的电话费单价
bool cmp(record r1, record r2)  //自己写的比较函数,按照名字聚合,然后内部再排
{
 if (r1.name != r2.name) return r1.name < r2.name;
 if (r1.month != r2.month) return r1.month < r2.month;
 if (r1.day != r2.day) return r1.day < r2.day;
 if (r1.hour != r2.hour) return r1.hour < r2.hour;
 else return r1.minute < r2.minute;
}
double pay(int d1, int h1, int m1, int d2, int h2, int m2)  //计算价格函数
{
 int time = 0;
 double money = 0;
 while (d1 != d2 || h1 != h2 || m1 != m2) {
  time++;
  m1++;
  money += price[h1];
  if (m1 == 60) {
   m1 = 0;
   h1++;
  }
  if (h1 == 24) {
   h1 = 0;
   d1++;
  }
 }
 printf("%d $%.2f\n", time, money / 100);
 return money / 100;
}
int main()
{
 int n;
 vector<record> res;   //记录所有账单
 for (int i = 0; i < 24; i++) scanf("%d", &price[i]);
 scanf("%d", &n);
 for (int i = 0; i < n; i++) {
  record r;
  cin >> r.name;
  scanf("%d:%d:%d:%d",&r.month, &r.day, &r.hour, &r.minute);
  cin >> r.state;
  res.push_back(r);
 }
 sort(res.begin(), res.end(), cmp);
 map<string, vector<record>> customer;  //对于每一个string name都对应一个vector数组,记录他的账单
 for (int i = 1; i < res.size(); i++) {
  if (res[i - 1].name == res[i].name && res[i - 1].state == "on-line" && res[i].state == "off-line") {
   customer[res[i - 1].name].push_back(res[i - 1]);
   customer[res[i - 1].name].push_back(res[i]);
  }
 }
 for (auto it : customer) {  //遍历图
  double total = 0;
  printf("%s %02d\n", it.first.c_str(), it.second[0].month);
  vector<record> r = it.second;
  for (int i = 1; i < r.size(); i+=2) {
   printf("%02d:%02d:%02d %02d:%02d:%02d ",r[i-1].day, r[i - 1].hour, r[i - 1].minute, r[i].day, r[i].hour, r[i].minute);
   total += pay(r[i - 1].day, r[i - 1].hour, r[i - 1].minute, r[i].day, r[i].hour, r[i].minute);
  }
  printf("Total amount: $%.2f\n", total);
 }
 return 0;
}
水平有限,如果代码有任何问题或者有不明白的地方,欢迎在留言区评论;也欢迎各位提出宝贵的意见!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值