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
题目大意
题目给定一组通话记录,要求把所有用户的有效通话记录计费。并将计费后的结果按照客户的名字的字母序排列后输出。有效通话记录是指on-line记录后紧接着off-line记录的用户通话记录。
解题思路
- 初始化计费数组(按时间费用不同);
- 获取通话记录,并用冒泡法预排序(也可以读取完成后直接排序),使通话记录按照用户名字、记录时间等排好序;
- 筛选有效记录并计费;
- 按用户名排序输出账单(无费用的用户不可输出);
- 结束输出并返回0值。
代码
#include<stdio.h>
#include<string.h>
#define MAXN 1010
struct Record{
char name[25];
int time;
int d,h,m;
int status;
}rec[MAXN];
int Sort[MAXN];
int Fee[24];
int Fee_Date,Fee_Hour[24];
int N,mo,minute;
void Swap(int i,int j){
int t;
t=Sort[i];
Sort[i]=Sort[j];
Sort[j]=t;
return;
}
void Init(){
int i;
for(i=0;i<24;i++){
scanf("%d",&Fee[i]);
Fee_Hour[i]=60*Fee[i];
Fee_Date+=Fee_Hour[i];
}//初始化费用数组
scanf("%d",&N);//读入N
getchar();
for(i=0;i<N;i++){
rec[i].status=-1;
Sort[i]=-1;
}//数组准备
return;
}
void Get_Record(){
int i,j;
char temp[10];
int d,h,m;
int c;
for(i=0;i<N;i++){
scanf("%s %d:%d:%d:%d %s",&rec[i].name,&mo,&d,&h,&m,&temp);
getchar();
rec[i].d=d;
rec[i].h=h;
rec[i].m=m;
rec[i].time=(d*24+h)*60+m;//标化分钟数
if(temp[1]=='n'){
rec[i].status=1;
}else{
rec[i].status=0;
}
Sort[i]=i;
//读入数据
for(j=i;j>0;j--){
c=strcmp(rec[Sort[j]].name,rec[Sort[j-1]].name);
if(c<0){
Swap(j,j-1);
}else if(c==0){
if(rec[Sort[j]].time<rec[Sort[j-1]].time){
Swap(j,j-1);
}else{
break;
}
}else{
break;
}
}//冒泡排序
}
}
double Get_Mon(int begin,int end){
int d,h,m;
int i;
double money=0.0;
minute=0;
minute+=(rec[end].d-rec[begin].d-1)*1440;
money+=(rec[end].d-rec[begin].d-1)*Fee_Date;
for(i=rec[begin].h+1;i<24;i++){
minute+=60;
money+=Fee_Hour[i];
}//按小时 (开始)
for(i=0;i<rec[end].h;i++){
minute+=60;
money+=Fee_Hour[i];
}//按小时 (结束)
for(i=rec[begin].m;i<60;i++){
minute++;
money+=Fee[rec[begin].h];
}//按分钟 (开始)
for(i=0;i<rec[end].m;i++){
minute++;
money+=Fee[rec[end].h];
}//按小时 (结束)
return money/100.0;
}
void Print(){
int i=0;
int begin=-1,end=-1;
int flag,P;
double sum,mon;
while(i<N){
flag=0;
P=0;
sum=0.0;
while(1){
if(flag==0){//需要begin,end扔掉
if(rec[Sort[i]].status==1){
begin=Sort[i];
flag=1;//需要end
}//
}else{//需要end,但拿到begin更新
if(rec[Sort[i]].status==0){
end=Sort[i];
flag=0;//需要begin
}else{
begin=Sort[i];
}
}
if(begin!=-1&&end!=-1){
if(P==0){
printf("%s %02d\n",rec[Sort[i]].name,mo);
P++;
}
mon=Get_Mon(begin,end);
sum+=mon;
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",rec[begin].d,rec[begin].h,rec[begin].m,rec[end].d,rec[end].h,rec[end].m,minute,mon);
begin=-1;
end=-1;
}
i++;
if(strcmp(rec[Sort[i]].name,rec[Sort[i-1]].name)!=0||i>=N){//不一样了
if(P==1){
printf("Total amount: $%.2lf\n",sum);
}
break;
}
}
}
return;
}
int main(){
Init();
Get_Record();
Print();
return 0;
}
运行结果