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. 在类中使用Response.Redirect()方法

    问题来自:"我在app_code 定义了user.cs类:其中作了跳转:Httpcontect.Current.Response.Redirect("/c/index.aspx&q ...

  2. loj #2024. 「JLOI / SHOI2016」侦查守卫

    #2024. 「JLOI / SHOI2016」侦查守卫   题目描述 小 R 和 B 神正在玩一款游戏.这款游戏的地图由 nnn 个点和 n−1n - 1n−1 条无向边组成,每条无向边连接两个点, ...

  3. Python flask虚拟环境安装

    1.安装virtualenv 2.在当前路径下创建文件夹,启动虚拟环境 3.在使用虚拟环境前需激活,前面出现(env说明在虚拟环境中).虚拟环境中默认安装了pip,所以直接pip安装flask 4.在 ...

  4. 明明有印象却找不到,APP内搜索为什么这么难用?

    赶上了互联网浪潮的当代人,每当有任何困扰,第一反应都是打开搜索引擎. 什么叫做“硬核相亲”,什么是“pick一下”,“达达主义”,“隐形贫困人口”——你都默默搜索过,不想被时代与话题抛弃.也许只有这样 ...

  5. 「洛谷P3768」简单的数学题 莫比乌斯反演+杜教筛

    题目链接 简单的数学题 题目描述 输入一个整数n和一个整数p,你需要求出 \[\sum_{i=1}^n\sum_{j=1}^n (i\cdot j\cdot gcd(i,j))\ mod\ p\]  ...

  6. numpy方法介绍

    三.numpy系列 1.np.maximum:(X, Y, out=None) X 与 Y 逐位比较取其大者: 最少接收两个参数 h=[[-2,2,10],[-5,-9,20]] hh=np.maxi ...

  7. 关于logrotate工具的日志切割

    logrotate是一个非常好的文件切割工具!! 具体配置如下: /var/log/debug.log{         daily            ; 每天转储         rotate ...

  8. eclipse字体

  9. 洛谷1541(多维dp)

    走格子拿分数,直接弄dp[i]是到了第i格的最大得分可以发现是假的. 于是此题设f[i][j][k][t]代表四种步伐各用了几次可以得到的最大得分,到达的点可以直接算出来,就好转移了. const i ...

  10. void类型指针的基本用法

    void作为指针时可以用任意类型的的指针值都可以给它进行赋值和传递,但是输出时必须时显性输出 代码如下: #include<cstdio> #include<iostream> ...