1016. Phone Bills (25)

来源:互联网 发布:黄江cnc编程招聘信息 编辑:程序博客网 时间:2024/05/22 06:50

1016. Phone Bills (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 1010CYLL 01:01:06:01 on-lineCYLL 01:28:16:05 off-lineCYJJ 01:01:07:00 off-lineCYLL 01:01:08:03 off-lineCYJJ 01:01:05:59 on-lineaaa 01:01:01:03 on-lineaaa 01:02:00:01 on-lineCYLL 01:28:15:41 on-lineaaa 01:05:02:24 on-lineaaa 01:04:23:59 off-line
Sample Output:
CYJJ 0101:05:59 01:07:00 61 $12.10Total amount: $12.10CYLL 0101:06:01 01:08:03 122 $24.4028:15:41 28:16:05 24 $3.85Total amount: $28.25aaa 0102:00:01 04:23:59 4318 $638.80Total amount: $638.80

提交代码

题目意思:

第一行给出一天内24小时资费,接着n,最后n行表示n个通话记录时间地点。每个通话记录都有姓名,月、日、时、分,以及通话开始还是结束,其中有些通话记录是无效的,每一个通话开始对应一个结束,通话开始和结束之间不能再出现其他的通话记录。输出要求:按姓名的字典序从小到大的顺序输出存在有效通话记录的用户,姓名和通话的月份只输出一次占一行,后面是每个符合要求的通话开始时间和结束时间,本次通话时间和产生的费用。最后需要输出所有通话的总费用。

解题思路:

根据输出要求,我们要进行一个排序。按姓名、月份、天、小时、分钟排序。排序之后依次遍历,判断两个通话记录的开始和结束是否相邻,并且开始在前,结束在后,符合条件记录下符合的次数,因为姓名和月份只需要输出一次。输出姓名和月份后,每次符合的通话记录的开始和结束都需要输出。当相邻两个记录不是同一个用户的时候sum和print全部置零。

姓名长度有坑,如果是20就会只能过第一个点。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 1010int a[25];struct node{  char name[30];//巨坑,20就只能过第一个点  int month,day,hour,minute;  bool status;}rec[N],temp;bool cmp(node a, node b){  int temp = strcmp(a.name,b.name);  if(temp != 0){    return temp < 0;  }  else if(a.month != b.month) {    return a.month < b.month;  }else if(a.day != b.day) {    return a.day < b.day;  }else if(a.hour != b.hour){    return a.hour < b.hour;  }else{    return a.minute < b.minute;  }}int getmoney(int on,int &time,int off){  time = 0;  int money = 0;  temp = rec[on];  while(temp.day < rec[off].day||temp.hour < rec[off].hour||temp.minute < rec[off].minute){    time++;//分钟加一    money+=a[temp.hour];    temp.minute++;//分钟到了60自动进入下一个小时    if(temp.minute >= 60){      temp.minute = 0;      temp.hour++;    }    //小时到了24自动进入下一天    if(temp.hour >= 24){      temp.hour = 0;      temp.day++;    }  }  return money;}int main(){  int n;  char str[10];//  freopen("input.txt","r",stdin);  for(int i = 0;i < 24;i++){    scanf("%d",&a[i]);  }  scanf("%d",&n);  for(int i = 0;i < n;i++){    scanf("%s%d:%d:%d:%d",rec[i].name,&rec[i].month,&rec[i].day,&rec[i].hour,&rec[i].minute);    scanf("%s",str);    if(strcmp(str,"on-line")==0){//标记开始还是结束      rec[i].status = true;    }else if(strcmp(str,"off-line")==0){      rec[i].status = false;    }  }  sort(rec,rec+n,cmp);  int sum = 0;//记录每个用户的总消费  int money = 0;//记录单次通话消费  int time = 0;  bool flag = false;//是否有符合条件的通话  int print = 0;//符合条件的通话的个数  for(int i = 0;i < n;i++){    if(rec[i].status&&!rec[i+1].status&&strcmp(rec[i].name,rec[i+1].name)==0){      flag = true;      print++;//符合条件的通话个数加一      money=getmoney(i,time,i+1);      sum+=money;    }    if(print == 1&&flag){//控制姓名和月份只输出一次      printf("%s %02d\n",rec[i].name,rec[i].month);    }    if(flag){      printf("%02d:%02d:%02d ",rec[i].day,rec[i].hour,rec[i].minute);      printf("%02d:%02d:%02d ",rec[i+1].day,rec[i+1].hour,rec[i+1].minute);      printf("%d $%.2lf\n",time,money*1.0/100);//份要转化为元      flag = false;    }    if(strcmp(rec[i].name,rec[i+1].name) != 0&&print != 0){      printf("Total amount: $%.2lf\n",sum*1.0/100);//该用户全部遍历完成,输出总消费和总通话时间      flag = false;      print = 0;//个数置零      sum = 0;//总金额置零    }  }  return 0;}


原创粉丝点击