1016. Phone Bills (25)
题目连接:https://www.patest.cn/contests/pat-a-practise/1016
原题如下:
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
这道题我感觉够难的,首先要经行排序,按照名字,时间;然后相邻两个得是一个on,一个0ff,不是这种的就可以的剔除了,是这种规格的记录时间并输出;
费用的计算我感觉是最难想的……参考了别人的解法,用总时间模24(得出小时)进行累加计算,然后将余数(即一天中的哪个时间段,分钟)再累加上即可
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include<stdbool.h> 5 #define MAXN 1005 6 int curtime=0; 7 float curmoney=0,Totalmoney=0; 8 int flag=0; 9 int visited[MAXN]={0}; 10 int Time[24]; 11 typedef struct CNode{ 12 char Name[25]; 13 int month; 14 int date; 15 int hours; 16 int minutes; 17 int tag; 18 double TT; 19 }ct; 20 21 ct Customer[MAXN]; 22 int cmp(const void *a,const void *b) 23 { 24 if (strcmp((*(ct*)a).Name,(*(ct*)b).Name)!=0)return (strcmp((*(ct*)a).Name,(*(ct*)b).Name)); //字典顺序(降序) 25 else if ((*(ct*)a).TT!=(*(ct*)b).TT)return ((*(ct*)a).TT-(*(ct*)b).TT); //时间升序 26 } 27 28 float chargeByTime(int total) 29 { 30 int i,hours,minutes; 31 float money=0; 32 hours=total/60; 33 minutes=total%60; 34 for (i=0;i<hours;i++)money+=Time[i%24]*60; 35 money+=Time[i%24]*minutes; 36 return money; 37 } 38 39 float Cost(ct s,ct t) 40 { 41 return ((chargeByTime(s.TT)-chargeByTime(t.TT))/100); 42 } 43 44 void Put(int i) 45 { 46 curtime=Customer[i+1].TT-Customer[i].TT; 47 curmoney=Cost(Customer[i+1],Customer[i]); 48 printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",Customer[i].date,Customer[i].hours,Customer[i].minutes, 49 Customer[i+1].date,Customer[i+1].hours,Customer[i+1].minutes,curtime,curmoney); 50 Totalmoney+=curmoney; 51 } 52 53 54 int main() 55 { 56 int i,j,N; 57 58 for (i=0;i<24;i++)scanf("%d",&Time[i]); 59 scanf("%d",&N); 60 char na[25],on[10]; 61 int mo,da,h,m; 62 for (i=0;i<N;i++) 63 { 64 scanf("%s%d%*c%d%*c%d%*c%d%*c%s",&na,&mo,&da,&h,&m,&on); 65 strcpy(Customer[i].Name,na); 66 Customer[i].month=mo;Customer[i].date=da; 67 Customer[i].hours=h;Customer[i].minutes=m; 68 Customer[i].TT=Customer[i].minutes+Customer[i].hours*60+Customer[i].date*60*24; 69 if (strcmp(on,"on-line")==0){Customer[i].tag=1;} 70 else if (strcmp(on,"off-line")==0){Customer[i].tag=0;} 71 getchar(); 72 } 73 74 qsort(Customer,N,sizeof(Customer[0]),cmp); 75 // i=2; 76 // printf("%s %02d:%02d:%02d:%02d",Customer[i].Name,Customer[i].month,Customer[i].date,Customer[i].hours,Customer[i].minutes); 77 78 char curname[25]; 79 for (i=0;i<N;i++) 80 { 81 82 if (!visited[i] && (strcmp(Customer[i].Name,Customer[i+1].Name)==0) && Customer[i].tag==1 && Customer[i+1].tag==0) 83 { 84 visited[i]=visited[i+1]=0; 85 if (i==N)break; 86 if (flag==0) 87 { 88 strcpy(curname,Customer[i].Name); 89 printf("%s %02d\n",Customer[i].Name,Customer[i].month); 90 flag=1; 91 } 92 if (strcmp(curname,Customer[i].Name)==0) 93 { 94 Put(i); 95 } 96 else if (strcmp(curname,Customer[i].Name)!=0) 97 { 98 printf("Total amount: $%.2f\n",Totalmoney); 99 Totalmoney=0; 100 strcpy(curname,Customer[i].Name); 101 printf("%s %02d\n",Customer[i].Name,Customer[i].month); 102 103 Put(i); 104 } 105 } 106 } 107 printf("Total amount: $%.2f\n",Totalmoney); 108 }