pat1016. Phone Bills (25)
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 (<= 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)的更多相关文章
- A1016 Phone Bills (25)(25 分)
A1016 Phone Bills (25)(25 分) A long-distance telephone company charges its customers by the followin ...
- 1016. Phone Bills (25)——PAT (Advanced Level) Practise
题目信息: 1016. Phone Bills (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A l ...
- PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)
1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following r ...
- 1016. Phone Bills (25) -vector排序(sort函数)
题目如下: A long-distance telephone company charges its customers by the following rules: Making a long- ...
- 1016 Phone Bills (25)(25 point(s))
problem A long-distance telephone company charges its customers by the following rules: Making a lon ...
- PAT A1016 Phone Bills (25)
题目描述 A long-distance telephone company charges its customers by the following rules: Making a long-d ...
- A1016 Phone Bills (25 分)
A long-distance telephone company charges its customers by the following rules: Making a long-distan ...
- 1016 Phone Bills (25 分)
A long-distance telephone company charges its customers by the following rules: Making a long-distan ...
- 1016 Phone Bills (25分)
复建的第一题 理解题意 读懂题目就是一个活,所以我们用观察输出法,可以看出来月份,以及时间和费用之间的关系. 定义过程 然后时间要用什么来记录呢?day hour minute 好麻烦呀..用字符串吧 ...
随机推荐
- 英文操作系统中中文乱码(SQL中 NVARCHAR 和 VARCHAR区别)
varchar在SQL Server中是采用单字节来存储数据的,nvarchar是使用Unico来存储数据的.中文字符存储到SQL Server中会保存为两个字节(一般采用Unico编码),英 ...
- [转]Marshaling a SAFEARRAY of Managed Structures by P/Invoke Part 4.
1. Introduction. 1.1 In parts 1 through 3 of this series of articles, I have thoroughly discussed th ...
- charles 抓取app https 请求
测试需要抓取app的https请求链接,百度了一下教程,能设置的都设置成功了,但就是抓取不成功,显示如下图 无奈之下还是用谷歌搜索了下(网速极慢),但是庆幸的找到了问题的答案,原因还是手机设置的问 打 ...
- 基于注解的AOP配置
配置文件 spring配置文件中的约束 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...
- Hawk-and-Chicken 强连通
题意:一群人投票 票具有传递性 求出累计和最大的数和 哪几个人最大 强连通好题!!! 毫无疑问先强连通缩点 一开始打算拓扑排序求dis 但是发现拓扑排序会有重复累加的情况 那么就反向建图 当 ...
- DP【洛谷P3135】[USACO16JAN]堡哞Fort Moo
[洛谷P3135][USACO16JAN]堡哞Fort Moo Bessie和她的朋友Elsie正在建筑一个堡垒,与任何一个好的堡垒一样,这个需要一个强固的框架.Bessie想造一个轮廓是1m宽的空心 ...
- WebForm与MVC混用 (转)
http://blog.csdn.net/leftfist/article/details/11591231
- Common Subsequence(最长公共子序列)
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. ...
- drozer与adb工具的安装与使用
drozer:链接: https://pan.baidu.com/s/1skTJdgh 密码: wah1 adb:链接: https://pan.baidu.com/s/1gfpIkuv 密码: n8 ...
- IDEA中Java代码存入DB中为乱码
有一种可能是编译后出现的乱码,可以在Setting的Java compiler中加如下 -encoding UTF-8