标签:
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:计算金钱的时候,可以完全采用模拟的方式这样可以简化很多计算 2:在数据中看是否有满足要求的数据,本代码采用了needprint方式,这种方式值得借鉴。
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 int rate[24]; 7 struct Record 8 { 9 char name[25]; 10 int month; 11 int day; 12 int hour; 13 int min; 14 char condition[12]; 15 }Record[1010]; 16 bool cmp(struct Record A,struct Record B) 17 { 18 if(strcmp(A.name,B.name)) 19 return strcmp(A.name,B.name)<0; 20 else 21 { 22 if(A.month!=B.month) 23 return A.month<B.month; 24 else if(A.day!=B.day) 25 return A.day<B.day; 26 else if(A.hour!=B.hour) 27 return A.hour<B.hour; 28 else 29 return A.min<B.min; 30 } 31 } 32 void Compute(struct Record &A,struct Record &B,int &time,int &money) 33 { 34 //正常模拟即可 此方法好 35 while(A.day<B.day||A.hour<B.hour||A.min<B.min) 36 { 37 time++; 38 money+=rate[A.hour]; 39 A.min++; 40 if(A.min==60) 41 { 42 A.hour++; 43 A.min=0; 44 } 45 if(A.hour==24) 46 { 47 A.day++; 48 A.hour=0; 49 } 50 } 51 52 } 53 int main(int argc, char *argv[]) 54 { 55 for(int i=0;i<24;i++) 56 scanf("%d",&rate[i]); 57 int count; 58 scanf("%d",&count); 59 for(int i=0;i<count;i++) 60 { 61 scanf("%s %d:%d:%d:%d",Record[i].name,&Record[i].month,&Record[i].day,&Record[i].hour,&Record[i].min); 62 scanf("%s",Record[i].condition); 63 } 64 sort(Record,Record+count,cmp); 65 for(int i=0;i<count;i++) //? 66 { 67 bool flag=false; 68 if(!strcmp(Record[i].name,Record[i+1].name)) 69 { 70 int cnt=0; 71 double total=0; 72 int need_print=0; 73 for(int j=i;j<count;j++) 74 { 75 if(!strcmp(Record[i].name,Record[j].name)) 76 { 77 cnt++; 78 } 79 } 80 for(int j=i;j<i+cnt;j++) 81 { 82 if(!strcmp(Record[j].condition,"on-line")&&need_print==0) 83 need_print=1; 84 if(!strcmp(Record[j].condition,"off-line")&&need_print==1) 85 need_print=2; 86 } 87 88 //如果有不匹配的怎么办?此处有问题 89 if(need_print==2) 90 { 91 printf("%s %02d\n",Record[i].name,Record[i].month); 92 for(int j=i;j<=i+cnt-1;j++) //change 93 { 94 if(j+1>i+cnt-1) 95 break; 96 if(!strcmp(Record[j].condition,"on-line")&&!strcmp(Record[j+1].condition,"off-line")) 97 { 98 //compute 99 printf("%02d:%02d:%02d ",Record[j].day,Record[j].hour,Record[j].min); 100 printf("%02d:%02d:%02d ",Record[j+1].day,Record[j+1].hour,Record[j+1].min); 101 int time=0,money=0; 102 Compute(Record[j],Record[j+1],time,money); 103 printf("%d $%.2f\n",time,money/100.0); 104 total+=money; 105 } 106 } 107 printf("Total amount: $%.2f\n",total/100); 108 } 109 i+=cnt-1; 110 } 111 } 112 return 0; 113 }
标签:
原文地址:http://www.cnblogs.com/GoFly/p/4285597.html