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
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef struct{
char id[];
int m, dd, hh, mm;
int on_line;
}info;
int N, rates[];
bool cmp(info a, info b){
if(strcmp(a.id, b.id) < )
return true;
else if(strcmp(a.id, b.id) == && a.m < b.m)
return true;
else if(strcmp(a.id, b.id) == && a.m == b.m && a.dd < b.dd)
return true;
else if(strcmp(a.id, b.id) == && a.m == b.m && a.dd == b.dd && a.hh < b.hh)
return true;
else if(strcmp(a.id, b.id) == && a.m == b.m && a.dd == b.dd && a.hh == b.hh && a.mm < b.mm)
return true;
else return false;
}
double counts(info a, info b, int &length){
double sum = ;
length = ;
while(a.dd < b.dd || a.hh < b.hh || a.mm < b.mm){
sum += rates[a.hh];
a.mm++;
length++;
if(a.mm == ){
a.mm = ;
a.hh++;
}
if(a.hh == ){
a.hh = ;
a.dd++;
}
}
return sum;
}
int main(){
info records[];
char id[] = {'\0'}, states[];
for(int i = ; i < ; i++)
scanf("%d", &rates[i]);
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%s %d:%d:%d:%d %s", records[i].id, &records[i].m, &records[i].dd, &records[i].hh, &records[i].mm, states);
if(strcmp(states, "on-line") == )
records[i].on_line = ;
else
records[i].on_line = ;
}
sort(records, records + N, cmp);
int prt = , length;
double charge = , temp;
for(int i = ; i < N - ; i++){
if(strcmp(records[i].id, records[i + ].id) == && records[i].on_line == && records[i + ].on_line == ){
if(prt == ){
printf("%s %02d\n", records[i].id, records[i].m);
prt = ;
}
temp = counts(records[i], records[i + ], length) / ;
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", records[i].dd, records[i].hh, records[i].mm, records[i + ].dd, records[i + ].hh, records[i + ].mm, length, temp);
charge += temp;
}else if(strcmp(records[i].id, records[i + ].id) != ){
if(prt == ){
printf("Total amount: $%.2f\n", charge);
}
charge = ;
prt = ;
}
}
if(prt == ){
printf("Total amount: $%.2f", charge);
}
cin >> N;
return ;
}

总结:

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。

A1016. Phone Bills的更多相关文章

  1. A1016 Phone Bills (25)(25 分)

    A1016 Phone Bills (25)(25 分) A long-distance telephone company charges its customers by the followin ...

  2. PAT A1016 Phone Bills (25 分)——排序,时序

    A long-distance telephone company charges its customers by the following rules: Making a long-distan ...

  3. PAT A1016 Phone Bills (25)

    题目描述 A long-distance telephone company charges its customers by the following rules: Making a long-d ...

  4. A1016 Phone Bills (25 分)

    A long-distance telephone company charges its customers by the following rules: Making a long-distan ...

  5. PAT甲级——A1016 Phone Bills

    A long-distance telephone company charges its customers by the following rules: Making a long-distan ...

  6. PTA A1016

    A1016 Phone Bills (25 分) 题目内容 A long-distance telephone company charges its customers by the followi ...

  7. PAT_A1016#Phone Bills

    Source: PAT A1016 Phone Bills (25 分) Description: A long-distance telephone company charges its cust ...

  8. PAT题目AC汇总(待补全)

    题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...

  9. 【算法学习记录-排序题】【PAT A1016】Phone Bills

    A long-distance telephone company charges its customers by the following rules: Making a long-distan ...

随机推荐

  1. js值----你所不知道的JavaScript系列(6)

    1.数组 在 JavaScript 中,数组可以容纳任何类型的值,可以是字符串.数字.对象(object),甚至是其他数组(多维数组就是通过这种方式来实现的) .----<你所不知道的JavaS ...

  2. 【nodejs】让nodejs像后端mvc框架(asp.net mvc)一orm篇【如EF般丝滑】typeorm介绍(8/8)

    文章目录 前情概要 在使用nodejs开发过程中,刚好碰到需要做一个小工具,需要用到数据库存储功能.而我又比较懒,一个小功能不想搞一个nodejs项目,又搞一个后端项目.不如直接在nodejs里面把对 ...

  3. BugkuCTF web基础$_GET

    前言 写了这么久的web题,算是把它基础部分都刷完了一遍,以下的几天将持续更新BugkuCTF WEB部分的题解,为了不影响阅读,所以每道题的题解都以单独一篇文章的形式发表,感谢大家一直以来的支持和理 ...

  4. Ionic 3 延迟加载(Lazy Load)实战(一)

    本文分享并演示了在 Ionic 3 框架中如何进行模块的延迟加载(Lazy Load)开发. 在我的实战课程「快速上手Ionic3 多平台开发企业级问答社区」中,因为开发的仿知乎 App 模块间的加载 ...

  5. ssh实现办公室电脑连接家中的电脑

    友情提示:如果您不知道您家路由器管理页面的密码,请您忽略此文. 问题背景: 家中有台笔记本电脑,它是通过家中的路由器与外界联网的,这时,我想通过ssh服务让公司的电脑能连上我家中的笔记本. 可以画个图 ...

  6. 11.14 Daily Scrum

    实现推荐菜谱时遇到问题,这个功能要和数据库和服务器有关,所以暂时放在一边,可能在beta版本实现. 部分成员已经完成基本任务,进行完善.   Today's Task Tomorrow's Task ...

  7. 《Linux内核设计与实现》 第十八章学习笔记

    调  试 一.准备开始 一个bug 一个藏匿bug的内核版本 相关内核代码的知识和运气 知道这个bug最早出现在哪个内核版本中. 1.想要成功进行调试: 让这些错误重现 抽象出问题 从代码中搜索 二. ...

  8. sqoop 使用笔记

    好久没有更新自己技术博客,现在开始工作了,把自己遇到的问题写到这里边来 主要把自己的问题写出来,分享给大家 sqoop 导入数据时候 有时候会遇到mysql 中有sql 中的关键字 这时候如果直接导出 ...

  9. Minimum Integer CodeForces - 1101A (思维+公式)

    You are given qq queries in the following form: Given three integers lili, riri and didi, find minim ...

  10. 『编程题全队』Scrum 冲刺博客

    1.介绍小组新加入的成员,Ta担任的角色 Answer: 我们小组的倪兢飞同学决定跳槽到团队あ,我们小组开了一个简短而又严肃的会议,满足倪兢飞同学的意愿,并感谢他为团队做出的巨大贡献.虽然我们遗失了一 ...