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. javascript 字典类型的使用

    javascript  字典类型的使用 1.使用Array: var arr = new Array(); arr["zs"] = "zhangsan"; ar ...

  2. gRPC官方文档(概念)

    文章来自gRPC 官方文档中文版 gRPC 概念 本文档通过对于 gRPC 的架构和 RPC 生命周期的概览来介绍 gRPC 的主要概念.本文是在假设你已经读过文档部分的前提下展开的.针对具体语言细节 ...

  3. ubuntu - 14.04,安装、配置GO语言开发工具Eclipse!!

    在配置Eclipse之前,我们必须保证下面这些都已经安装,并且正常工作了: 一,Go语言:参考文章 http://blog.csdn.net/sunylat/article/details/49859 ...

  4. laravel安装[转https://laravelacademy.org/post/9528.html]

    Laravel 框架对PHP版本和扩展有一定要求,不过这些要求 Laravel Homestead 都已经满足了,不过如果你没有使用 Homestead 的话(那真是一件很遗憾的事情),有必要了解下这 ...

  5. SIGHUP信号

    SIGHUP会在以下3种情况下被发送给相应的进程:1.终端关闭时,该信号被发送到session首进程以及作为job提交的进程(即用 & 符号提交的进程)2.session首进程退出时,该信号被 ...

  6. Qt 学习之路 2(8):添加动作

    Home / Qt 学习之路 2 / Qt 学习之路 2(8):添加动作       [在WINDOWS10 QTCREATOR MENU添加无效]   Qt 学习之路 2(8):添加动作  豆子   ...

  7. [Maven]How do I tell Maven to use the latest version of a dependency?

    Link: http://stackoverflow.com/questions/30571/how-do-i-tell-maven-to-use-the-latest-version-of-a-de ...

  8. Django工程创建

    方法一: 1.win+r进入cmd命令窗口: 2.找到Django的安装地址: 3.cmd窗口中利用cd 进入相应的文件夹,再输入命令如下: django-admin.exe startproject ...

  9. C语言中变量、全局变量与变量的作用域

    什么是变量: 变量常量都是表征数据的一种形式:常量用来表示数据的值: 变量不仅可以用来表示数据的值:还可以用来存放数据:因为变量对应着一定的内存单元: 变量和常量必须先定义后使用. 变量名和常量名都是 ...

  10. jq自定义鼠标右键菜单

    效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...