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-lineSample 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;
。
方法一:
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<queue> 6 #include<vector> 7 #include<cmath> 8 #include<string> 9 using namespace std; 10 struct record{ 11 string name,state; 12 int d,h,m; 13 }; 14 int timecost[24]; 15 bool cmp(record a,record b){//all desc 16 if(a.name==b.name){ 17 if(a.d==b.d){ 18 if(a.h==b.h){ 19 return a.m<b.m; 20 } 21 return a.h<b.h; 22 } 23 return a.d<b.d; 24 } 25 return a.name<b.name; 26 } 27 double getCost(record a){ 28 int h=a.d*24+a.h; 29 int m=a.m; 30 int i; 31 double sum=0; 32 for(i=0;i<h;i++){ 33 sum+=timecost[i%24]; 34 } 35 sum*=60; 36 sum+=timecost[i%24]*m; 37 return sum/100; 38 } 39 double calCost(record a,record b){ 40 return getCost(b)-getCost(a); 41 } 42 int calTime(record a,record b){ 43 return (b.d-a.d)*1440+(b.h-a.h)*60+b.m-a.m; 44 } 45 //record w[1005]; 46 int main(){ 47 //freopen("D:\INPUT.txt","r",stdin); 48 int i; 49 for(i=0;i<24;i++){ 50 scanf("%d",&timecost[i]); 51 } 52 int m,mon; 53 scanf("%d",&m); 54 record *w=new record[m+5]; 55 for(i=0;i<m;i++){ 56 cin>>w[i].name; 57 scanf("%d:%d:%d:%d",&mon,&w[i].d,&w[i].h,&w[i].m); 58 cin>>w[i].state; 59 } 60 sort(w,w+m,cmp); 61 62 /*for(i=0;i<m;i++){ 63 cout<<w[i].name<<" "<<w[i].d<<" "<<w[i].h<<" "<<w[i].m<<" "<<w[i].state<<endl; 64 }*/ 65 66 int j; 67 string name,pname=""; 68 bool hav=false; 69 double sum=0; 70 for(i=0;i<m;i++){ 71 if(!hav&&w[i].state=="on-line"){ 72 hav=true; 73 name=w[i].name; 74 } 75 else if(hav&&w[i].state=="on-line"){//update 76 name=w[i].name; 77 } 78 else if(hav&&w[i].state=="off-line"&&w[i].name==name){ 79 hav=false; 80 if(name!=pname){ 81 if(pname!=""){ 82 printf("Total amount: $%.2lf ",sum); 83 } 84 sum=0; 85 pname=name; 86 cout<<pname; 87 printf(" %02d ",mon); 88 } 89 double partsum=calCost(w[i-1],w[i]); 90 sum+=partsum; 91 printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf ",w[i-1].d,w[i-1].h,w[i-1].m,w[i].d,w[i].h,w[i].m,calTime(w[i-1],w[i]),partsum); 92 } 93 } 94 if(sum)//如果题目不能保证至少有一对满足条件,加上此判断条件照样成立 95 printf("Total amount: $%.2lf ",sum); 96 delete []w; 97 return 0; 98 }
方法二:
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<queue> 6 #include<vector> 7 #include<cmath> 8 #include<string> 9 using namespace std; 10 struct record{ 11 string name,state; 12 int d,h,m; 13 }; 14 int timecost[24]; 15 bool cmp(record a,record b){//all desc 16 if(a.name==b.name){ 17 if(a.d==b.d){ 18 if(a.h==b.h){ 19 return a.m<b.m; 20 } 21 return a.h<b.h; 22 } 23 return a.d<b.d; 24 } 25 return a.name<b.name; 26 } 27 //record w[1005]; 28 int main(){ 29 //freopen("D:\INPUT.txt","r",stdin); 30 int i,dcost=0; 31 for(i=0;i<24;i++){ 32 scanf("%d",&timecost[i]); 33 dcost+=timecost[i]*60; 34 } 35 int m,mon; 36 scanf("%d",&m); 37 record *w=new record[m+5]; //段错误容易出现 38 for(i=0;i<m;i++){ 39 cin>>w[i].name; 40 scanf("%d:%d:%d:%d",&mon,&w[i].d,&w[i].h,&w[i].m); 41 cin>>w[i].state; 42 } 43 sort(w,w+m,cmp); 44 45 /*for(i=0;i<m;i++){ 46 cout<<w[i].name<<" "<<w[i].d<<" "<<w[i].h<<" "<<w[i].m<<" "<<w[i].state<<endl; 47 }*/ 48 49 int j,total; 50 string name; 51 for(i=0;i<m;){ 52 name=w[i].name; 53 if(i<m-1&&w[i].name==name&&w[i+1].name==name&&w[i].state=="on-line"&&w[i+1].state=="off-line"){ 54 cout<<name; 55 printf(" %02d ",mon); 56 total=0; 57 } 58 else{ 59 i++; 60 continue; 61 } 62 for(j=i;j<m-1&&w[j].name==name&&w[j+1].name==name;j++){ 63 if(w[j].state=="on-line"&&w[j+1].state=="off-line"){ 64 int d,h,part=0,timemin=0; 65 if(w[j].d==w[j+1].d){//同一天 66 if(w[j].h==w[j+1].h){ 67 timemin+=w[j+1].m-w[j].m; 68 part+=timecost[w[j].h]*timemin; 69 } 70 else{ 71 timemin=60-w[j].m; 72 part+=timecost[w[j].h]*timemin; 73 for(h=w[j].h+1;h<w[j+1].h;h++){ 74 timemin+=60; 75 part+=60*timecost[h]; 76 } 77 timemin+=w[j+1].m; 78 part+=timecost[w[j+1].h]*w[j+1].m; 79 } 80 } 81 else{//非同一天 82 timemin+=60-w[j].m; 83 part+=timecost[w[j].h]*timemin; 84 for(h=w[j].h+1;h<24;h++){ 85 timemin+=60; 86 part+=timecost[h]*60; 87 } 88 for(d=w[j].d+1;d<w[j+1].d;d++){ 89 timemin+=24*60; 90 part+=dcost; 91 } 92 for(h=0;h<w[j+1].h;h++){ 93 timemin+=60; 94 part+=timecost[h]*60; 95 } 96 timemin+=w[j+1].m; 97 part+=timecost[w[j+1].h]*w[j+1].m; 98 } 99 printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf ",w[j].d,w[j].h,w[j].m,w[j+1].d,w[j+1].h,w[j+1].m,timemin,part*1.0/100); 100 total+=part; 101 j++; 102 } 103 } 104 printf("Total amount: $%.2lf ",total*1.0/100); 105 if(w[j].name!=name){ 106 i=j; 107 } 108 else{ 109 i=j+1; 110 } 111 } 112 delete []w; 113 return 0; 114 }