1016. Phone Bills (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代码

注意点:

1.段错误:说明内存使用有问题,尽量避免使用new delete。如果要用,申请的空间相对于题意大一些。

2.如果某人没有有效通话记录,则不输出该人的信息。

3.通话时间钱的计算:假设我们计算time1到time2的账单。

(1)我们可以采用从起点(即00:00:00)开始计算,结果就是get_money(time2) - get_money(time1), 这样计算方便。

(2)我们也可以采用从time1开始递增直到time2, 这样比较烦。

4.有效的通话记录是指:如果某人的通话记录为1.on;2.on;3.off;,则其中1.on将被抛弃,匹配到2.on;3.off;

方法一:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
using namespace std;
struct record{
string name,state;
int d,h,m;
};
int timecost[];
bool cmp(record a,record b){//all desc
if(a.name==b.name){
if(a.d==b.d){
if(a.h==b.h){
return a.m<b.m;
}
return a.h<b.h;
}
return a.d<b.d;
}
return a.name<b.name;
}
double getCost(record a){
int h=a.d*+a.h;
int m=a.m;
int i;
double sum=;
for(i=;i<h;i++){
sum+=timecost[i%];
}
sum*=;
sum+=timecost[i%]*m;
return sum/;
}
double calCost(record a,record b){
return getCost(b)-getCost(a);
}
int calTime(record a,record b){
return (b.d-a.d)*+(b.h-a.h)*+b.m-a.m;
}
//record w[1005];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int i;
for(i=;i<;i++){
scanf("%d",&timecost[i]);
}
int m,mon;
scanf("%d",&m);
record *w=new record[m+];
for(i=;i<m;i++){
cin>>w[i].name;
scanf("%d:%d:%d:%d",&mon,&w[i].d,&w[i].h,&w[i].m);
cin>>w[i].state;
}
sort(w,w+m,cmp); /*for(i=0;i<m;i++){
cout<<w[i].name<<" "<<w[i].d<<" "<<w[i].h<<" "<<w[i].m<<" "<<w[i].state<<endl;
}*/ int j;
string name,pname="";
bool hav=false;
double sum=;
for(i=;i<m;i++){
if(!hav&&w[i].state=="on-line"){
hav=true;
name=w[i].name;
}
else if(hav&&w[i].state=="on-line"){//update
name=w[i].name;
}
else if(hav&&w[i].state=="off-line"&&w[i].name==name){
hav=false;
if(name!=pname){
if(pname!=""){
printf("Total amount: $%.2lf\n",sum);
}
sum=;
pname=name;
cout<<pname;
printf(" %02d\n",mon);
}
double partsum=calCost(w[i-],w[i]);
sum+=partsum;
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",w[i-].d,w[i-].h,w[i-].m,w[i].d,w[i].h,w[i].m,calTime(w[i-],w[i]),partsum);
}
}
if(sum)//如果题目不能保证至少有一对满足条件,加上此判断条件照样成立
printf("Total amount: $%.2lf\n",sum);
delete []w;
return ;
}

方法二:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
using namespace std;
struct record{
string name,state;
int d,h,m;
};
int timecost[];
bool cmp(record a,record b){//all desc
if(a.name==b.name){
if(a.d==b.d){
if(a.h==b.h){
return a.m<b.m;
}
return a.h<b.h;
}
return a.d<b.d;
}
return a.name<b.name;
}
//record w[1005];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int i,dcost=;
for(i=;i<;i++){
scanf("%d",&timecost[i]);
dcost+=timecost[i]*;
}
int m,mon;
scanf("%d",&m);
record *w=new record[m+]; //段错误容易出现
for(i=;i<m;i++){
cin>>w[i].name;
scanf("%d:%d:%d:%d",&mon,&w[i].d,&w[i].h,&w[i].m);
cin>>w[i].state;
}
sort(w,w+m,cmp); /*for(i=0;i<m;i++){
cout<<w[i].name<<" "<<w[i].d<<" "<<w[i].h<<" "<<w[i].m<<" "<<w[i].state<<endl;
}*/ int j,total;
string name;
for(i=;i<m;){
name=w[i].name;
if(i<m-&&w[i].name==name&&w[i+].name==name&&w[i].state=="on-line"&&w[i+].state=="off-line"){
cout<<name;
printf(" %02d\n",mon);
total=;
}
else{
i++;
continue;
}
for(j=i;j<m-&&w[j].name==name&&w[j+].name==name;j++){
if(w[j].state=="on-line"&&w[j+].state=="off-line"){
int d,h,part=,timemin=;
if(w[j].d==w[j+].d){//同一天
if(w[j].h==w[j+].h){
timemin+=w[j+].m-w[j].m;
part+=timecost[w[j].h]*timemin;
}
else{
timemin=-w[j].m;
part+=timecost[w[j].h]*timemin;
for(h=w[j].h+;h<w[j+].h;h++){
timemin+=;
part+=*timecost[h];
}
timemin+=w[j+].m;
part+=timecost[w[j+].h]*w[j+].m;
}
}
else{//非同一天
timemin+=-w[j].m;
part+=timecost[w[j].h]*timemin;
for(h=w[j].h+;h<;h++){
timemin+=;
part+=timecost[h]*;
}
for(d=w[j].d+;d<w[j+].d;d++){
timemin+=*;
part+=dcost;
}
for(h=;h<w[j+].h;h++){
timemin+=;
part+=timecost[h]*;
}
timemin+=w[j+].m;
part+=timecost[w[j+].h]*w[j+].m;
}
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",w[j].d,w[j].h,w[j].m,w[j+].d,w[j+].h,w[j+].m,timemin,part*1.0/);
total+=part;
j++;
}
}
printf("Total amount: $%.2lf\n",total*1.0/);
if(w[j].name!=name){
i=j;
}
else{
i=j+;
}
}
delete []w;
return ;
}

pat1016. Phone Bills (25)的更多相关文章

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

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

  2. 1016. Phone Bills (25)——PAT (Advanced Level) Practise

    题目信息: 1016. Phone Bills (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A l ...

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

    1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following r ...

  4. 1016. Phone Bills (25) -vector排序(sort函数)

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

  5. 1016 Phone Bills (25)(25 point(s))

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

  6. PAT A1016 Phone Bills (25)

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

  7. A1016 Phone Bills (25 分)

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

  8. 1016 Phone Bills (25 分)

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

  9. 1016 Phone Bills (25分)

    复建的第一题 理解题意 读懂题目就是一个活,所以我们用观察输出法,可以看出来月份,以及时间和费用之间的关系. 定义过程 然后时间要用什么来记录呢?day hour minute 好麻烦呀..用字符串吧 ...

随机推荐

  1. Hyper-V 中遇到错误 'vm' could not initialize

    不知道前面做了什么操作,但当我启动我的虚拟机时遇到了'vm' could not initialize的错误,如下图: 在几番尝试未果后,找到了如下解决方法: 以管理员模式启动cmd,执行命令 net ...

  2. org.apache.commons.lang3包中的isEmpty和isBlank

    主要为了区分一下empty和blank的用法,先看源码: isEmpty public static boolean isEmpty(CharSequence cs) { return cs == n ...

  3. 解决:kali linux 在vmware 虚拟机中使用bridge模式上网的问题

    安装kali后,使用独立ip上网,但是设置bridge模式后依然上不了网,后来查了好多资料才解决。 能ping通网页,能ping通DNS,就是不能打开网页。 最后的原因是主机的防火墙拦截,把防火墙关了 ...

  4. vue添加新属性不更新原因

    一: 在我们使用vue进行开发的过程中,可能会遇到一种情况:当生成vue实例后,当再次给数据赋值时,有时候并不会自动更新到视图上去: 当我们去看vue文档的时候,会发现有这么一句话:如果在实例创建之后 ...

  5. 洛谷 P1801 黑匣子_NOI导刊2010提高(06)

    题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两种: ...

  6. pacman命令用法

    Pacman 是一个命令行工具,这意味着当你执行下面的命令时,必须在终端或控制台中进行. 1.更新系统 在 Arch Linux 中,使用一条命令即可对整个系统进行更新: pacman -Syu 如果 ...

  7. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_.Net Framework 部署目标

    1.解决Windows性能不稳定: 2.降低Windows程序安装的复杂性: 3.解决Windows程序不安全性: 4.解决应用程序状态在硬盘上分散: 5.允许用户灵活地控制哪些东西能够安装,哪些东西 ...

  8. 计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle

    Problem Description There is a collector who own many valuable jewels. He has a problem about how to ...

  9. redis 集合

    > SADD myset1 a b c (integer) > SADD web maiziedu.com (integer) > SADD web maiziedu.com (in ...

  10. Go语言基础之21--反射

    一.变量介绍 1.1 变量的内在机制 A. 类型信息,这部分是元信息,是预先定义好的:比如:string.int等 B. 值类型,这部分是程序运行过程中,动态改变的:比如:是变量实例所存储的真是的值. ...