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 好麻烦呀..用字符串吧 ...
随机推荐
- Algorithms - Insertion sort
印象 图1 插入排序过程 思想 插入排序(Insertion Sort)的主要思想是不断地将待排序的元素插入到有序序列中,是有序序列不断地扩大,直至所有元素都被插入到有序序列中. 分析 时间复杂度: ...
- PowerDesigner 16.5 链接SQL Server 2008R2
链接的目的主要是为了使用PowerDesigner反向工程生成数据字典 Step1: 在Workspace中创建New Physical Data Model, 否则不会出现Database菜单 当链 ...
- oracle sql 字段行转列
数据库中原先如图: 现在要吧WHMM行转列: conncect by用于确定列的个数
- Google Maglev 牛逼的网络负载均衡器(转)
https://segmentfault.com/a/1190000009565788 Maglev 是什么 Maglev 是谷歌搞的一个工作在三层(IP层)的网络负载均衡器, 它是一个运行在普通的 ...
- 用户、组或角色 'zgb' 在当前数据库中已存在。 (Microsoft SQL Server,错误: 15023)
在使用SQL Server 时,我们经常会遇到一个情况:需要把一台服务器上的数据库转移到另外一台服务器上.而转移完成后,需要给一个"登录"关联一个"用户"时,往 ...
- P2488 [SDOI2011]工作安排 费用流
\(\color{#0066ff}{ 题目描述 }\) 你的任务是制定出一个产品的分配方案,使得订单条件被满足,并且所有员工的愤怒值之和最小.由于我们并不想使用Special Judge,也为了使选手 ...
- 有趣的数 zoj 月赛
题目描述 让我们来考虑1到N的正整数集合.让我们把集合中的元素按照字典序排列,例如当N=11时,其顺序应该为:1,10,11,2,3,4,5,6,7,8,9. 定义K在N个数中的位置为Q(N,K),例 ...
- vue.js路由嵌套传参
通过配置路由时候按照: path:/user/:username/age/:age 这种就可以把参数传递 接受: $routes.params 接受到的是一个json格式的数据,
- java中\r ,\n,\t等的含义
\t 相当于tab,缩进,制表符\n NewLine 换行 \f 换页符 \r 回车 \" 转义 “\\ 转义 \
- git学习---去除版本控制
本地这样去除文件夹 node_modules 的版本关联:执行:git rm -r --cached "node_modules/"提交: git commit -am 'remo ...