题目:
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分析:
这一题有点繁琐,但是思路很明确。首先就是对所有的record排序,先按名字排序,如果名字一样就按时间排序。
排序之后就一条一条的过滤,只有当前后两条记录名字一样且前一条为on后一条为off的时候才进行处理。
如果一条记录是有效的,那么就计算cost,计算cost非常麻烦,比如通话时间可能跨天(⊙﹏⊙b汗),再者由于不同时段的价格不一样,所以如果计算时间差才来算cost的时候很容易错。我一开始一直wa就是因为用时间追赶的方式计算cost。最后也没找出来错误。所以这里使用另外一个方法,就是两个时间都从00:00:00开始计算价格,然后再用第二个减去第一个,我把cost换成后者之后马上后三个case全过了。。。。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _record
{
char name[25];
char time[15];
char type[10];
}record;
int cmp(const void *a,const void *b);
int rate[24];
int rate_total;
record r[1001];
int main(int argc,char* argv[])
{
int i,j,n,minute,cost,cost1,cost2,total,dd1,dd2,hh1,hh2,mm1,mm2;
char name[25],month[3],tmp[25];
//freopen("input","r",stdin);
rate_total = 0;
for(i=0;i<24;i++){
scanf("%d",&rate[i]);
rate_total += rate[i]*60;
}
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s%s%s",r[i].name,r[i].time,r[i].type);
for(i=0;i<2;i++)
month[i] = r[0].time[i];
month[i] = '\0';
qsort(r,n,sizeof(record),cmp);
/*for(i=0;i<n;i++)
printf("%s %s %s\n",r[i].name,r[i].time,r[i].type);*/
i = 0;
strcpy(name,r[0].name);
total = 0;
while(i < n-1)
{
//if name not match
if(strcmp(r[i].name,name))
{
if(total > 0){
printf("Total amount: $%.2f\n",total/100.0);
total = 0;
}
strcpy(name,r[i].name);
}
//find on-line
if(!strcmp(r[i].type,"on-line"))
{
//match name and on-line
if(!strcmp(r[i].name,r[i+1].name) && !strcmp(r[i+1].type,"off-line"))
{
if(strcmp(name,tmp)){
printf("%s %s\n",name,month);
strcpy(tmp,name);
}
printf("%s %s ",r[i].time+3,r[i+1].time+3);
//print minute
dd1 = (r[i].time[3]-'0')*10 + (r[i].time[4]-'0');
dd2 = (r[i+1].time[3]-'0')*10 + (r[i+1].time[4]-'0');
hh1 = (r[i].time[6]-'0')*10 + (r[i].time[7]-'0');
hh2 = (r[i+1].time[6]-'0')*10 + (r[i+1].time[7]-'0');
mm1 = (r[i].time[9]-'0')*10 + (r[i].time[10]-'0');
mm2 = (r[i+1].time[9]-'0')*10 + (r[i+1].time[10]-'0');
minute = (dd2*1440+hh2*60+mm2) - (dd1*1440+hh1*60+mm1);
printf("%d ",minute);
//print cost
cost1 = cost2 =0;
cost1 += dd1*rate_total;
cost2 += dd2*rate_total;
for(j=0;j<hh1;j++)
cost1 += rate[j]*60;
cost1 += rate[j]*mm1;
for(j=0;j<hh2;j++)
cost2 += rate[j]*60;
cost2 += rate[j]*mm2;
cost = cost2-cost1;
printf("$%.2f\n",cost/100.0);
total += cost;
i += 1;
}
//this record is on-line but next record is not off-line
else
i += 1;
}
//this record is off-line
else
i += 1;
}
if(total > 0)
printf("Total amount: $%.2f\n",total/100.0);
return 0;
}
int cmp(const void *a,const void *b)
{
if( strcmp((*(record*)a).name,(*(record *)b).name)>0)
return 1;
else if( strcmp((*(record*)a).name,(*(record *)b).name)<0 )
return -1;
else
{
if( strcmp((*(record*)a).time,(*(record *)b).time)>0 )
return 1;
else
return -1;
}
}