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:
1 2 3 4 5 6 7 8 9 10 11 12 |
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:
1 2 3 4 5 6 7 8 9 10 |
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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<memory.h> #define ONLINE 2 #define OFFLINE 1 #define TABLE_SIZE 2011 int toll[24]; typedef struct t_ { int month; int day; int hour; int minute; } t; typedef struct list_ { t start; t end; int is_done; struct list_ *next; } list; typedef struct hashtable_ { char name[21]; int state; list *bill; list *bill_tail; } hashtable; int hashing(char *name) { int temp=0,i=0; while(*name) { temp=(temp<<1)+*name; name++; } return temp%TABLE_SIZE; } int compare(const void *a,const void *b) { if(((hashtable *)a)->state==0) { return 1; } else if(((hashtable *)b)->state==0) { return -1; } else { return strcmp((char *)&(((hashtable *)a)->name),(char *)&(((hashtable *)b)->name)); } } int calculate(t *start,t *end) { return 0;//temporary expression for testing } int cost(t *start,t *end) { return 0;//temporary expression for testing } int main() { int n,i,offset,pos,state,current,total,fee,time_cost; t temp_time; list *temp; hashtable customer[TABLE_SIZE]; char name[21],state_buf[9]; for(i=0;i<24;i++) { scanf("%d",toll+i); } scanf("%d",&n); for(i=0;i<TABLE_SIZE;i++) { customer[i].state=0; } for(i=0;i<n;i++) { scanf("%s%d:%d:%d:%d%s",&name,&(temp_time.month),&(temp_time.day),&(temp_time.hour),&(temp_time.minute),&state_buf); offset=0; pos=hashing(name); while(customer[pos].state!=0) { if(!strcmp(customer[pos].name,name)) { break; } pos+=(offset<<2)+1; offset++; } if(state_buf[1]=='n') { state=ONLINE; } else { state=OFFLINE; } if(customer[pos].state==0&&state==ONLINE) { strcpy(customer[pos].name,name); customer[pos].state=ONLINE; customer[pos].bill=(list *)malloc(sizeof(list)); memcpy(&(customer[pos].bill->start),&temp_time,sizeof(t)); customer[pos].bill->next=NULL; customer[pos].bill_tail=customer[pos].bill; customer[pos].bill_tail->is_done=0; } else if(state==ONLINE) { customer[pos].state=ONLINE; if(customer[pos].bill_tail->is_done==1) { temp=(list *)malloc(sizeof(list)); temp->next=NULL; customer[pos].bill_tail->next=temp; } memcpy(&(customer[pos].bill_tail->start),&temp_time,sizeof(t)); customer[pos].bill_tail->is_done=0; } else if(customer[pos].state==ONLINE&&state==OFFLINE) { customer[pos].state=OFFLINE; memcpy(&(customer[pos].bill_tail->end),&temp_time,sizeof(t)); customer[pos].bill_tail->is_done=1; } } qsort(customer,TABLE_SIZE,sizeof(hashtable),compare); for(i=0;i<TABLE_SIZE;i++) { if(customer[i].state==0) { break; } current=0; total=0; while(customer[i].bill!=NULL) { if(customer[i].bill->is_done==1) { if(current!=customer[i].bill->start.month) { if(total!=0) { printf("Total amount: $%d.%02d\n",total/100,total%100); } current=customer[i].bill->start.month; printf("%s %02d\n",customer[i].name,current); total=0; } fee=calculate(&(customer[i].bill->start),&(customer[i].bill->end)); time_cost=cost(&(customer[i].bill->start),&(customer[i].bill->end)); printf("%02d:%02d:%02d %02d:%02d:%02d %d $%d.%02d\n", customer[i].bill->start.day,customer[i].bill->start.hour,customer[i].bill->start.minute, customer[i].bill->end.day,customer[i].bill->end.hour,customer[i].bill->end.minute, time_cost,fee/100,fee%100); total+=fee; } customer[i].bill=customer[i].bill->next; } printf("Total amount: $%d.%02d\n",total/100,total%100); } } |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
#include<stdio.h> #include<stdlib.h> #include<string.h> #define ONLINE 1 #define OFFLINE 0 #define UNAVAILABLE -1 int toll[24]; typedef struct clog_ { char name[21]; int month; int day; int hour; int minute; int state; } clog; int compare(const void *a,const void *b) { int value; value=strcmp(((clog *)a)->name,((clog *)b)->name); return value?value: ( ((clog *)a)->month-((clog *)b)->month?((clog *)a)->month-((clog *)b)->month: ( ((clog *)a)->day-((clog *)b)->day?((clog *)a)->day-((clog *)b)->day: ( ((clog *)a)->hour-((clog *)b)->hour?((clog *)a)->hour-((clog *)b)->hour: ( ((clog *)a)->minute-((clog *)b)->minute ) ) ) ); } int check(clog *t) { int sum,i; sum=0; for(i=0;i<t->hour;i++) { sum+=toll[i]*60; } sum+=toll[i]*t->minute; return sum; } int calculate(clog *end,clog *start) { int extra,i; extra=0; if(end->day-start->day>0) { for(i=0;i<24;i++) { extra+=toll[i]*60; } extra*=(end->day-start->day); } return check(end)-check(start)+extra; } int sub(clog *end,clog *start) { int end_m,start_m; end_m=end->day*24*60+end->hour*60+end->minute; start_m=start->day*24*60+start->hour*60+start->minute; return end_m-start_m; } int main() { clog *customer; char buf[9]; int n,i,flag=0,total,charge,lasting,last=-1; for(i=0;i<24;i++) { scanf("%d",toll+i); } scanf("%d",&n); customer=(clog *)malloc(n*sizeof(clog)); for(i=0;i<n;i++) { scanf("%s%d:%d:%d:%d%s",customer[i].name,&(customer[i].month),&(customer[i].day), &(customer[i].hour),&(customer[i].minute),buf); if(buf[1]=='n') { customer[i].state=ONLINE; } else { customer[i].state=OFFLINE; } } qsort(customer,n,sizeof(clog),compare); for(i=0;i<n;i++) { if(customer[i].state==ONLINE&&i<n-1&&!strcmp(customer[i].name,customer[i+1].name)&&customer[i+1].state==OFFLINE) { i+=1; } else { customer[i].state=UNAVAILABLE; } } for(i=0;i<n;i++) { if(customer[i].state!=UNAVAILABLE) { if(last==-1||strcmp(customer[last].name,customer[i].name)) { if(last!=-1) { printf("Total amount: $%d.%02d\n",total/100,total%100); } printf("%s %02d\n",customer[i].name,customer[i].month); total=0; } if(customer[i].state==ONLINE) { charge=calculate(customer+i+1,customer+i); lasting=sub(customer+i+1,customer+i); printf("%02d:%02d:%02d %02d:%02d:%02d %d $%d.%02d\n", customer[i].day,customer[i].hour,customer[i].minute, customer[i+1].day,customer[i+1].hour,customer[i+1].minute, lasting,charge/100,charge%100); i+=1; last=i; total+=charge; } } } if(last!=-1) { printf("Total amount: $%d.%02d\n",total/100,total%100); } } |