PTA A1016
A1016 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.
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
单词
bills
英 /bɪls/ 美 /bɪls/
n. 账单;议案(bill的复数)
n. (Bills)人名;(英、德)比尔斯
v. 开账单(bill的第三人称单数形式)
calendar
英 /'kælɪndə/ 美 /'kæləndɚ/
n. 日历;[天] 历法;日程表
vt. 将…列入表中;将…排入日程表
denoting
指示(denote的现在分词)
toll
英 /təʊl/ 美 /tol/
vt. 征收;敲钟
n. 通行费;代价;钟声;伤亡人数
vi. 鸣钟;征税
cent
英 /sent/ 美 /sɛnt/
n. 分;一分的硬币;森特(等于半音程的百分之一)
n. (Cent)人名;(法)桑
chronologically
英 /krɔnə'lɔdʒikli/ 美 /krɔnə'lɔdʒikli/
adv. 按年代地
paired
英 /peəd/ 美 /peəd/
adj. [计] 成对的;配对的
v. 使成对;配合(pair的过去分词)
alphabetical
英 /ælfə'betɪk(ə)l/ 美 /,ælfə'bɛtɪkl/
adj. 字母的;[计] 依字母顺序的
题目分析
计算花费账单,这题主要是排序和计算花费,排序可以使用qsort,我昨天还特地复习了一下,写了篇博客【C/C++】qsort函数的使用方法和细节。
先根据名字顺序排序,然后在每个名字序列你根据时间排序(为了方便比较我把时间全部转化成了分钟数),排好序后先判断这个用户有没有有效记录,如果有根据是否有两个相邻且状态不同的记录,如果是就输出开始时间和结束时间计算分钟数,计算账单金额。
关于计算账单金额,看上去很简单,其实细分起来情况还是很多的,我一开始就是分情况做的,什么一个小时内的情况,一天内多个个小时的情况,跨天可是小时数不满一天,好多天的情况,这些情况都对应着一些极端数据,不考虑肯定是A不了的,弄起来很麻烦。后来我用了一个最佛系的方法,一分钟一分钟的遍历.....最后我得出一个结论,能简单解决的问题就不要分情况瞎折腾。
具体代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ONLINE 0
#define OFFLINE 1
int fare[24];
struct record
{
char user[21];
int min;
int line_char;
};
int N, M;
struct record r[1001];
int convert_min(int d, int h, int m)
{
return d * 24 * 60 + h * 60 + m;
}
int compare_user(const void *p,const void *q)
{
return strcmp(((struct record*)p)->user, ((struct record*)q)->user);
}
int compare_min(const void *p, const void *q)
{
return ((struct record*)p)->min - ((struct record*)q)->min;
}
int main(void)
{
for (int i = 0; i < 24; i++)
scanf("%d", &fare[i]);
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
int d, h, m;
char line[10];
scanf("%s %d:%d:%d:%d %s", r[i].user, &M, &d, &h, &m, line);
r[i].min = convert_min(d, h, m);
if (strcmp(line, "on-line") == 0)
r[i].line_char = ONLINE;
else
r[i].line_char = OFFLINE;
}
qsort(r, N, sizeof(struct record), compare_user);
int begin = 0;
char* temp = r[begin].user;
int dayfare = 0;
for (int i = 0; i < 24; i++)
dayfare += fare[i];
for (int i = 0; i <= N; )
{
if (strcmp(temp, r[i].user) != 0)
{
qsort(r + begin, i - begin, sizeof(struct record), compare_min);
int flag = 0;
for (int n = begin; n < i; )
{
if (r[n].line_char == ONLINE && r[n + 1].line_char == OFFLINE && n + 1 < i)
{
flag = 1;
n += 2;
}
else n++;
}
if (flag)
{
printf("%s %02d\n", r[begin].user, M);
int sum = 0;
for (int n = begin; n < i; )
{
if (r[n].line_char == ONLINE && r[n + 1].line_char == OFFLINE && n + 1 < i)
{
printf("%02d:%02d:%02d", r[n].min / (24 * 60), r[n].min % (24 * 60) / 60, r[n].min % (24 * 60) % 60);
printf(" %02d:%02d:%02d", r[n + 1].min / (24 * 60), r[n + 1].min % (24 * 60) / 60, r[n + 1].min % (24 * 60) % 60);
printf(" %d", r[n + 1].min - r[n].min);
int mon = 0;
int start = r[n].min; int end = r[n + 1].min;
while (start != end )
{
mon += fare[(start % (60 * 24) / 60)];
start++;
}
sum += mon;
printf(" $%0.2f\n", mon / 100.0);
n += 2;
}
else n++;
}
printf("Total amount: $%0.2f\n", sum / 100.0);
}
begin = i;
temp = r[begin].user;
}
else i++;
}
system("pause");
}
参考博客
PTA A1016的更多相关文章
- 浙大PTA - - 堆中的路径
题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21731 本题即考察最小堆的基本操作: #include "iostrea ...
- 浙大PTA - - File Transfer
题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21732 #include "iostream" #includ ...
- ERROR<53761> - Plugins - conn=-1 op=-1 msgId=-1 - Connection Bind through PTA failed (91). Retrying...
LDAP6.3在DSCC控制台启动实例完成,但是操作状态显示“意外错误”,查看日志如下: 04/May/2016:21:10:39 +0800] - Sun-Java(tm)-System-Direc ...
- PTA中提交Java程序的一些套路
201708新版改版说明 PTA与2017年8月已升级成新版,域名改为https://pintia.cn/,官方建议使用Firefox与Chrome浏览器. 旧版 PTA 用户首次在新版系统登录时,请 ...
- PTA分享码-Java
主要用于Java语法练习,非竞赛类题目. 1. Java入门 959dbf0b7729daa61d379ec95fb8ddb0 2. Java基本语法 23bd8870e ...
- C语言第一次实验报告————PTA实验1.2.3内容
一.PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度100°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...
- PTA题---求两个有序序列中位数所体现的思想。
---恢复内容开始--- 近日,在做PTA题目时,遇到了一个这样的题,困扰了很久.题目如下:已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数.有序序列A0,A1, ...
- 第十四,十五周PTA作业
1.第十四周part1 7-3 #include<stdio.h> int main() { int n; scanf("%d",&n); int a[n]; ...
- 第七周PTA作业
第一题: #include<stdio.h> int main() { ; ; ){ sum=sum+i; i++; } printf("sum = %d\n",sum ...
随机推荐
- cloudstack 安装 install for ubuntu
准备工作环境信息 修改dns配置 设置阿里源root@sh-saas-cs-manager-online-01:~# mv /etc/apt/sources.list /etc/apt/sources ...
- webpack配置css浏览器前缀
webpack打包时,css自动添加浏览器前缀.我们需要用到一个Loader:postcss-loader,和一个插件:autoprefixer 安装 npm i postcss-loader aut ...
- Vintage_坏客户定义
python信用评分卡建模(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_ca ...
- CSAGAN的几大重点 - 2
1.生成器 1)MRU(SketchyGAN) 计算过程为: 与DCGAN[46]和ResNet生成架构的定性和定量比较可以在5.3节中找到.MRU块有两个输入:输入特征图xi和图像I,输出特征图yi ...
- java面试题实战二
1.spring 是如何创建bean的? 在IoC容器中,bean的获取主要通过BeanFactory和ApplicationContext获取,这里ApplicationContext实际上是继承自 ...
- 查找算法(3)--Interpolation search--插值查找
1. 插值查找 (1)说明 在介绍插值查找之前,首先考虑一个新问题,为什么上述算法一定要是折半,而不是折四分之一或者折更多呢? 打个比方,在英文字典里面查“apple”,你下意识翻开字典是翻前面的书页 ...
- BATJ都爱问的多线程面试题
# 一 面试中关于 synchronized 关键字的 5 连击 ### 1.1 说一说自己对于 synchronized 关键字的了解 synchronized关键字解决的是多个线程之间访问资源的同 ...
- Qt编写安防视频监控系统(界面很漂亮)
一.前言 视频监控系统在整个安防领域,已经做到了烂大街的程序,全国起码几百家公司做过类似的系统,当然这一方面的需求量也是非常旺盛的,各种定制化的需求越来越多,尤其是这几年借着人脸识别的东风,发展更加迅 ...
- shell基础知识8-xargs命令
简介 xargs 命令应该紧跟在管道操作符之后.它使用标准输入作为主要的数据源,将从 stdin 中 读取的数据作为指定命令的参数并执行该命令. 将多行输入转换成单行输出 [root@dns-node ...
- EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器功能简介---视频直播、直播鉴权(如何完美将EasyDSS过渡到新版)
作为RTMP流媒体服务器,接受RTMP推流.进行实时的直播流分发是EasyDSS流媒体服务自身一大核心功能.写本篇博文的一个目的是向大家介绍EasyDSS新版的直播间.匿名直播.和虚拟直播的功能, 另 ...