PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

时间:2021-10-03 00:35:27
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.

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-linerecord. 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

0  、1测试点:没有消费的用户不能输出

2测试点:on和off日期不在同一天却在同一个小时

3测试点:on和off日期同一天同一个小时

题意:

一个长途电话公司收费规则如下:

长途电话每分钟会有固定的收费,这取决于打电话时的时间。当一个顾客的长途电话接通时,这一刻的时间会记录下来,同样顾客挂断电话时也是这样。每个月,每分钟通话的电话账单就会寄给顾客。你的工作就是准备每月的账单,给出一系列电话通话记录。

每一个测试样例都有两个部分:费率结构和通话记录

费率结构包含了一行24个非负整数记录了一天24个小时每小时的长途电话费(美分/每分钟)。

下一行包含了一个正数N(≤1000),之后有N条记录,每一条电话通话记录包含了顾客的姓名(一个20个字符的字符串,没有空格) 时间(mm:dd:hh:mm)和文字on-line或者是off-line

对于每一个测试样例,所有的数据都是一个月内的。每一条on-line记录会有按时间先后排列的下一条记录(相同顾客名)状态为off-line和它构成一对。若有些on-line记录并没有可以配对的off-line记录,则忽视这条记录,同样若只有off-line记录,没有与之对应的on-line记录也忽视。可以保证至少有一条电话记录能够配对。你可以假设在同一时间同一顾客不会有两条记录。使用24小时时钟来记录时间

对于每一个测试样例,你需要为每一个顾客打印出电话账单

电话账单打印顺序按照顾客姓名的字母表顺序。对每一个顾客,首先打印出顾客名字和账单的月份。然后对每一个通话期间,用一行打印出开始时间和结束时间和日期(dd:hh:mm),持续时间(以分钟为单位) ,收费。所有通话按照时间先后顺序列出。最后打印出这个月的总费用

思路:

1.先把所有数据排个序,先按名字排,再时间

2.遍历,用个is_out标记是不是已经匹配且输出过(用于判断该不该打印Total money),用last记录上一次的状态,如果上一次状态是on-line且这次是off-line,那么{

  再判断有没有答应过名字(is_out)

  然后开始算时间和钱:时间和钱不用1分钟1分钟遍历的算,先让on的天数和小时数和off的相等,然后再减去多算的小时数,最后考虑分钟

  //分钟
  money-=last.m*p[last.h];//多的减去,注意是 p[last.h]
  money+=a[i].m*p[a[i].h];//少的加上,注意是 p[a[i].h]

}

3.字符串那里月份的处理卡了。。。哭泣

把整数加到字符串上

#include<bits/stdc++.h>
using namespace std;
int main(){ string s="";
s+="";
int a=;
s+=a+'';
cout<<s<<endl;
return ;
}

4.is_out是换一个名字就要初始化的,忘了。。。难过。。。

最终满分代码:

#include<bits/stdc++.h>
using namespace std;
struct node{
string name;
int d;
int h;
int m;
string state;
}a[];
bool cmp(node &x,node &y){
if(x.name==y.name){
if(x.d==y.d){
if(x.h==y.h){
return x.m<y.m;
}else{
return x.h<y.h;
}
}else{
return x.d<y.d;
}
}else{
return x.name<y.name;
}
} int main()
{
double p[];
double sum_p=;
for(int i=;i<=;i++){
cin>>p[i];
p[i]*=0.01;
sum_p+=p[i]*;
}
int n;
int month;
cin>>n;
for(int i=;i<=n;i++){
cin>>a[i].name;
scanf("%2d:%2d:%2d:%2d",&month,&a[i].d,&a[i].h,&a[i].m);
cin>>a[i].state;
}
sort(a+,a++n,cmp);
/*for(int i=1;i<=n;i++){
cout<<a[i].name<<" "<<a[i].d<<" "<<a[i].h<<" "<<a[i].m<<" "<<a[i].state<<endl;
}*/
node last;
last.name="";
int is_out=;//坑点!是否有匹配上且输出的
double totel=;
for(int i=;i<=n;i++){
if(i==){
last.name=a[i].name;
last.d=a[i].d;
last.h=a[i].h;
last.m=a[i].m;
last.state=a[i].state;
is_out=;
continue;
}
if(a[i].name!=last.name){
if(is_out)
{
//结算前一个人
printf("Total amount: $%.2lf\n",totel);
totel=;
}
is_out=; //这句导致第1,2个测试点过不了,名字一旦不同,不管有没有输出过,is_out要初始化
}else{
//看看状态对不对的上,对不上为"";
if(last.state=="on-line"&&a[i].state=="off-line"){
//计算时间
int t=a[i].d-last.d;
int t1=t**;
int t2=(a[i].h-last.h)*;
int t3=(a[i].m-last.m);
//计算bill,不需要一分钟一分钟的跑,找到规律即可
double money=;
money+=t*sum_p;
//小时
if(a[i].h>last.h)//比大小
{
for(int j=last.h;j<a[i].h;j++){
money+=p[j]*;
}
}
else{
for(int j=a[i].h;j<last.h;j++){
money-=p[j]*;
}
}
//分钟
money-=last.m*p[last.h];//多的减去,注意是 p[last.h]
money+=a[i].m*p[a[i].h];//少的加上,注意是 p[a[i].h]
//如果这个人没有被输出过
if(is_out==)
{
cout<<a[i].name<<" ";
printf("%02d\n",month);
is_out=;//匹配上且输出了
}
printf("%02d:%02d:%02d %02d:%02d:%02d ",last.d,last.h,last.m,a[i].d,a[i].h,a[i].m);
printf("%d $%.2lf\n",t1+t2+t3,money);
totel+=money;
}
}
last.name=a[i].name;
last.d=a[i].d;
last.h=a[i].h;
last.m=a[i].m;
last.state=a[i].state;
}
if(is_out){//匹配上的才输出
printf("Total amount: $%.2lf\n",totel);
}
return ;
}

我自己编的测试数据


aaa ::: off-line
aaa ::: off-line
aaa ::: on-line
aaa ::: on-line aaa ::: on-line
aaa ::: off-line
aaa ::: off-line
aaa ::: off-line aaa ::: on-line
aaa ::: off-line CYLL ::: on-line
CYLL ::: off-line
CYJJ ::: off-line
CYLL ::: off-line
CYJJ ::: on-line
aaa ::: on-line
aaa ::: on-line
CYLL ::: on-line
aaa ::: on-line
aaa ::: off-line CYJJ ::: off-line
CYJJ ::: on-line
CYJJ ::: on-line
CYJJ ::: off-line CYLL ::: on-line
CYLL ::: off-line
CYLL ::: on-line
CYLL ::: off-line
CYLL ::: on-line
CYLL ::: off-line
CYLL ::: on-line
CYLL ::: on-line
CYLL ::: off-line
CYLL ::: off-line
CYLL ::: off-line
CYLL ::: off-line
MQ ::: on-line
MQ ::: on-line
MQ ::: on-line
MQ ::: off-line
YF ::: on-line
YF ::: on-line

PAT 甲级 1016 Phone Bills (25 分)  (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

看着真舒服!!!

 

PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)的更多相关文章

  1. PAT 甲级 1028&period; List Sorting &lpar;25&rpar; 【结构体排序】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1028 思路 就按照 它的三种方式 设计 comp 函数 然后快排就好了 但是 如果用 c++ ...

  2. PAT 甲级 1022 Digital Library &lpar;30 分&rpar;(字符串读入getline&comma;istringstream&comma;测试点2时间坑点&rpar;

    1022 Digital Library (30 分)   A Digital Library contains millions of books, stored according to thei ...

  3. PAT甲级1016&period; Phone Bills

    PAT甲级1016. Phone Bills 题意: 长途电话公司按以下规定向客户收取费用: 长途电话费用每分钟一定数量,具体取决于通话时间.当客户开始连接长途电话时,将记录时间,并且客户挂断电话时也 ...

  4. PAT 甲级 1080 Graduate Admission &lpar;30 分&rpar; (简单,结构体排序模拟)

    1080 Graduate Admission (30 分)   It is said that in 2011, there are about 100 graduate schools ready ...

  5. PAT 乙级 1085&period; PAT单位排行 &lpar;25&rpar; 【结构体排序】

    题目链接 https://www.patest.cn/contests/pat-b-practise/1085 思路 结构体排序 要注意几个点 它的加权总分 是 取其整数部分 也就是 要 向下取整 然 ...

  6. PAT A 1016&period; Phone Bills &lpar;25&rpar;【模拟】

    题目:https://www.patest.cn/contests/pat-a-practise/1016 思路:用结构体存储,按照名字和日期排序,然后先判断是否有效,然后输出,时间加减直接暴力即可 ...

  7. PAT 甲级 1020 Tree Traversals &lpar;25分&rpar;(后序中序链表建树,求层序)&ast;&ast;&ast;重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  8. PAT 甲级 1146 Topological Order &lpar;25 分&rpar;&lpar;拓扑较简单,保存入度数和出度的节点即可&rpar;

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  9. PAT 甲级 1071&&num;160&semi;Speech Patterns&&num;160&semi;&lpar;25&&num;160&semi;分&rpar;(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

随机推荐

  1. cocos初认识

    一直知道cocos是做游戏的,想学习一下,结果去官网一看就懵逼了.Cocos Creator,Cocos2d-x,cocos studio,Cocos2d-js,Cocos2d-x-lua,那一种才是 ...

  2. npm isArray源码

    module exports的是一个函数. !! val是为了将val转化成布尔值. var isArray = Array.isArray; /** * toString */ var str = ...

  3. 从电商平台促销活动看电商app开发趋势

    据亿合科技小编了解到:尽管各大电商平台都进入了品质和品牌时代,但对于消费者来说,低价依然是一个有吸引力的因素.尼尔森<网络购物者趋势研究>报告显示,2016年价格敏感型购物者的比例从15% ...

  4. javaweb在线预览

    需要工具: 1.openoffice 将word.excel.ppt.txt等文件转换为pdf文件 2.SWFTool 将pdf转换为swf文件 3.flexPaper是一个开源轻量级的在浏览器上显示 ...

  5. mysql数据库指令导入导出

    mysql数据库导出使用  /mysql/bin/mysqldump指令 数据库导出 导出指令: [mysql安装目录]/bin/mysqldump -u[username] -p[password] ...

  6. Web Service--第一次接触web service

    Web Service 首发于开源中国 1. 背景 中国移动短信网关需求,要能够发送短信.开发材料只有一个短信发送配置:包括ID,password,code,url.一个jar包还有一个老旧的html ...

  7. ajax-javascript原生-初步入门01(整理)

    -----------------------------------2017.07.21写----------------------------------------- 1.ajax的原名: a ...

  8. Pyinstaller打包selenium去除chromedriver黑框问题解决!!!

    Pyinstaller打包selenium去除chromedriver黑框问题解决!!!     问题描述 [1123/101706.932:ERROR:gpu_process_transport_f ...

  9. DQL-联合查询

    一.含义union:合并.联合,将多次查询结果合并成一个结果二.语法查询语句1union [all]查询语句2union [all]... 三.意义1.将一条比较复杂的查询语句拆分成多条语句2.适用于 ...

  10. Git 项目提交代码及一些常用命令

    在dev_ysg分支 : git add . //把项目添加到仓库 git commit -m "test" // 提交加注释 git push //推到dev_ysg分支上去 g ...