• 1 题目
  • 2 解题思路
  • 3 AC代码
  • 4 总结

1 题目

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

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805493648703488

2 解题思路

  题目大意:给你一个长途电话收费标准,以及一张通话记录,计算每个客户的话费单。每张通话记录可能有不匹配的状况,你需要在on-line和off-line中找到匹配的一次完整的通话,然后开始计费。输出每次完整通话的开始和结束时间以及本次通话话费,然后再计算本月通话总计。
  这是一道排序题,但是需要注意的细节比较多,所以相比于一般的排序,也不简单。所以我觉得,与其所示排序,不如说是一道逻辑题。
  思路:
  1.题目要求以字母排序(alphabetical order)输出排序,但是从输出的Sample中可以看出,其实是以ASCII顺序,大写字母在前(6590),小写字母在后(97122)
  2.以时间进行排序,题目给出的是月、日、时、分(mm:dd:hh:mm)的格式,不妨统一转换成分钟的相对值(以本月1号0点0分为起始时间),方便排序和计算通话时长
  3.计费也是同样的道理,计算每一次通话的相对值(相对于每月1号0点0分起)的话费,然后两次计费相减,即一次完整通话的话费。这样计费标准可以直接存在一个数组里,for循环遍历相对时间就可以得到每一次通话记录的相对话费了。
  4.按姓名和通话时长排序之后,那么相邻的一次“on-line”和“off-line”必然是一次完整的通话。找到匹配通话记录之后,相减两者的“相对话费”,得到一次完整通话的话费,遍历有序记录,得到所有客户的数据。
  
  实现细节:可以把每个通话记录作为一个结构体,然后每个结构存储一个姓名、月、日、时、分,以及相对时间(分钟),结构体对象用vector存储,方便操作(也可以直接用结构体数组,但是比较麻烦),由于两个通话记录(on-line和off-line)对应一个客户名称,完成一次完整通话,不妨使用一个map来记录映射关系。

3 AC代码

/*
** @Brief:No.1016 of PAT advanced level.
** @Author:Jason.Lee
** @Date:2018-7-30
*/#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;struct records{string name;int status,month,time,day,hour,minute;
};bool cmp(records a,records b){return a.name!=b.name?a.name<b.name:a.time<b.time;
}// 计算每一个通话记录的相对开销
double billFromZero(records call,int*rate){double total = rate[call.hour]*call.minute+rate[24]*60*call.day;for(int i=0;i<call.hour;i++){total +=rate[i]*60;}return total/100.0;
}int main(){int rate[25]={0},N;for(int i=0;i<24;i++){scanf("%d",&rate[i]);rate[24]+=rate[i];}scanf("%d",&N);vector<records> data(N);for(int i=0;i<N;i++){cin>>data[i].name;scanf("%d:%d:%d:%d",&data[i].month,&data[i].day,&data[i].hour,&data[i].minute);string temp;cin>>temp;data[i].status = (temp=="on-line")?1:0;// 换算成分钟,方便排序以及最后结果计算 data[i].time = data[i].day*24*60+data[i].hour*60+data[i].minute;}sort(data.begin(),data.end(),cmp);map<string,vector<records>>customer;for(int i=1;i<N;i++){// 匹配符合条件的一对记录 if(data[i].name==data[i-1].name&&data[i-1].status==1&&data[i].status==0){customer[data[i-1].name].push_back(data[i-1]);customer[data[i].name].push_back(data[i]);}}for(auto it:customer){vector<records> temp = it.second;cout<<it.first;printf(" %02d\n",temp[0].month);double total = 0.0f;for(int i=1;i<temp.size();i+=2){// 通过相对花费差额来计算真实花费 double t = billFromZero(temp[i],rate) - billFromZero(temp[i-1],rate);printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",temp[i-1].day,temp[i-1].hour,temp[i-1].minute,temp[i].day,temp[i].hour,temp[i].minute,temp[i].time - temp[i-1].time,t);total+=t; }printf("Total amount: $%.2f\n",total);} return 0;
}

4 总结

  熟练使用STL能够提升生产效率,还能够减少错误。

1016 Phone Bills (25)(25 分)相关推荐

  1. 易基因 | 微量样本DNA甲基化测序分析怎么做?25.617分文章告诉你

    大家好,这里是专注表观组学十余年,领跑多组学科研服务的易基因. 想知道微量细胞样本的DNA甲基化测序分析怎么做吗?这篇25.617分的文章告诉你! 微量细胞样本Micro DNA-BS绘制猕猴植入前胚 ...

  2. 求与下面谓词公式等值的前束范式_在一阶逻辑中将下面命题符号化,并求出公式的前束范式 不是所有的火车都比所有的汽车跑到快。 (25.0分)_学小易找答案...

    [单选题]<荷塘月色>第五段讲了几层意思,请选出最恰当的一项() [简答题]5张与服装有关的牛仔元素面料再造,10张牛仔面料再造的文创作品 [单选题]A-B=B 的充分必要条件是 (5.0 ...

  3. PAT甲级1016 Phone Bills :[C++题解]字符串处理(复杂题)(C语言格式化读入、输出很便利!!!)

    文章目录 题目分析 题目链接 题目分析 原题: 长途电话公司按以下规则向客户收费: 拨打长途电话每分钟要花费一定的费用,具体收费取决于拨打电话的时间. 客户开始拨打长途电话的时间将被记录,客户挂断电话 ...

  4. C++学习之路 | PTA乙级—— 1016 部分A+B (15分)(精简)

    1016 部分A+B (15分) 正整数 A 的"D ​A ​​ (为 1 位整数)部分"定义为由 A 中所有 D ​A ​​ 组成的新整数 P ​A ​​ .例如:给定 A=38 ...

  5. 【注意点分析】1016 Phone Bills (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A long-distance telephone company charges its customers by the fo ...

  6. PAT 1016 Phone Bills (25分) 逻辑较为复杂 sort() + map

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

  7. 1016 Phone Bills (25 分) 【未完成】【难度: 中 / 知识点: 模拟】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805493648703488

  8. 1016 Phone Bills (25分)

    这个题相对复杂一点,读题用了20分钟,然后就是疯狂写代码,其实这个主要是计算费用复杂一点,其他倒是还好,第一遍提交有测试点1,2没过,回头看了一眼代码觉得计算不可能出错,然后一分析就是测试点1,2很显 ...

  9. 1016. Phone Bills (25)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 去掉非法数据计算账单 A long-distance telep ...

最新文章

  1. 算法:搜索插入的位置
  2. boost安装_编译安装Mysql详细步骤
  3. 复选框全选效果,根据单个复选框的选择情况确定全选复选框是否被选
  4. 我创意很大,玩转《猫和老鼠》手游,瓜分40万大奖
  5. uniny 物体运动到一个点停止_人教版高中英语必修五Unit 5 单词详解
  6. 休眠后gpio状态_STM32中GPIO的8种工作模式总结
  7. 【快速乘与快速幂例题讲解】相乘取余爆long long?试试快速乘吧!
  8. c语言程序设计辅导资料pdf,C语言程序设计辅导资料(修订版).pdf
  9. filezilla server mysql_使用FileZilla Server轻松搭建个人FTP服务器
  10. 软件架构设计-软件架构风格、分层架构
  11. 幂级数展开求积分_蛮力硬推定积分基本公式
  12. spring boot实现验证码登录
  13. 祝cattom考研成功
  14. WordPress主题_大前端DUX主题7.1原版+优化-91apps.cn就要应用网
  15. excel如何打开100万行以上的csv文件
  16. LeetCode 881. 救生艇
  17. Elasticsearch7.8
  18. hive 数据导入 导出
  19. 通达云OA2015版及钉钉、微信办公集成产品正式发布
  20. 【Matplotlib】三维图及其俯视图+colorbar的位置调整与颜色、刻度细化

热门文章

  1. 搭建自己Kindle电子书图书馆,并可远程访问
  2. 随路vld vs. 时钟gating
  3. kettle java环境变量,kettle环境变量配置
  4. 集成学习精讲之Boosting - SAP大神黄佳新作《零基础学机器学习》节选
  5. 单片机毕设分享100例(五)
  6. 传智黑马java基础学习——day07(基本语法的练习)
  7. 修改Transmission登陆密码
  8. (C语言)求不重复的最长子串
  9. centos 安装 kong
  10. AFL(american fuzzy lop)学习一