1016. Phone Bills (25)

来源:互联网 发布:java实验指导书答案 编辑:程序博客网 时间:2024/04/25 02:52

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
坑点:如果某人没有有效通话记录,则不输出该人的信息
//16:47#include <stdio.h>#include <queue>#include <stdlib.h>#include <string.h>#define MAXN 1000using namespace std;typedef struct{char name[25];int mon;int day;int hh;int mm;char state[10];}RECORD;typedef struct{int start_day;int start_hh;int start_mm;int end_day;int end_hh;int end_mm;int spendtime;int cost;}BILL2;typedef struct{char name[25];int amount;//总费用int mon;//通话所在月份queue<BILL2> q;//用户通话情况(起止时间,通话时间,话费)队列}BILL1;RECORD records[MAXN];BILL1 bills1[MAXN];int rate[24];int cmp1(const void* a,const void* b){//快排所使用的排序规则//如果日期一样,就比较小时;小时一样,就比较分钟//从小到大排序const RECORD* x = (const RECORD*)a;const RECORD* y = (const RECORD*)b;if(x->day < y->day)return -1;else if(x->day > y->day)return 1;else if(x->hh < y->hh)return -1;else if(x->hh > y->hh)return 1;else if(x->mm < y->mm)return -1;else return 1;}void presort(int n){int i,j,cnt;RECORD t;//姓名排序不使用qsort,因为快排不稳定//使用的起泡排序for(i = 0;i < n;i++){for(j = 0;j < n - 1 - i;j++){if(strcmp(records[j].name,records[j+1].name) > 0){t = records[j];records[j] = records[j+1];records[j+1] = t;}}}i = 1;if(n > 1){//当用例有多个通话记录才进行组内排序cnt = 1;while(i < n){if(strcmp(records[i-1].name,records[i].name) == 0){//统计每个用户的通话记录数cnt++;}else{if(cnt > 1){//当某个用户有多个通话记录时,才对该用户的通话时间进行排序qsort(records+i-cnt,cnt,sizeof(RECORD),cmp1);}cnt = 1;}i++;}if(cnt > 1){//对最后一个用户排序qsort(records+i-cnt,cnt,sizeof(RECORD),cmp1);}}}int costCalc(int start_day,int start_hh,int start_mm,int end_day,int end_hh,int end_mm){//通过通话起止时间计算话费int cnt = 0,cost = 0;while(!(start_day == end_day && start_hh == end_hh && start_mm == end_mm)){start_mm++;cnt++;if(start_mm == 60){//当分钟数满60时,计算一次费用cost += cnt * rate[start_hh];start_hh++;start_mm = 0;cnt = 0;}if(start_hh == 24){start_day++;start_hh = 0;}}cost += cnt * rate[start_hh];return cost;}int isNotExist(const char* p,int user_cnt){//所有已经计算过费用和通话时间等的用户的信息保存在bills1数组中//判定p代表的用户是否在已经处理过的用户列表中int i;for(i = 0;i < user_cnt;i++)if(strcmp(p,bills1[i].name) == 0)return 0;return 1;}int main(){int i,N,user_cnt;BILL2 bill2;for(i = 0;i <  24;i++){scanf("%d",&rate[i]);}scanf("%d",&N);for(i = 0;i < N;i++){scanf("%s %d:%d:%d:%d %s",&records[i].name,&records[i].mon,&records[i].day,&records[i].hh,&records[i].mm,&records[i].state);}//对数据进行预排序//排序规则:首先按照姓名进行字典排序,姓名相同时,按照时间先后排序presort(N);user_cnt = 0;for(i = 0;i < N - 1;i++){if(isNotExist(records[i].name,user_cnt)){//如果没有对该用户进行计算,则将其加入计算组strcpy((char*)&bills1[user_cnt].name,(const char*)&records[i].name);bills1[user_cnt].mon = records[i].mon;bills1[user_cnt].amount = 0;user_cnt++;}if(strcmp(records[i].name,records[i+1].name) == 0){//将符合规定的通话记录进行计算,并加入用户通话情况队列if(strcmp("on-line",records[i].state) == 0 &&strcmp("off-line",records[i+1].state) == 0){bill2.start_day = records[i].day;bill2.start_hh = records[i].hh;bill2.start_mm = records[i].mm;bill2.end_day = records[i+1].day;bill2.end_hh = records[i+1].hh;bill2.end_mm = records[i+1].mm;bill2.spendtime = (bill2.end_day - bill2.start_day)*24*60 + (bill2.end_hh - bill2.start_hh)*60 + (bill2.end_mm - bill2.start_mm);bill2.cost = costCalc(bill2.start_day,bill2.start_hh,bill2.start_mm,bill2.end_day,bill2.end_hh,bill2.end_mm);bills1[user_cnt-1].amount += bill2.cost;bills1[user_cnt-1].q.push(bill2);i++;}}}for(i = 0;i < user_cnt;i++){if(bills1[i].q.empty())//没有有效通话记录的用户不进行输出continue;printf("%s %02d\n",bills1[i].name,bills1[i].mon);while(!bills1[i].q.empty()){bill2 = bills1[i].q.front();printf("%02d:%02d:%02d ",bill2.start_day,bill2.start_hh,bill2.start_mm);printf("%02d:%02d:%02d ",bill2.end_day,bill2.end_hh,bill2.end_mm);printf("%d $%.2f\n",bill2.spendtime,(float)bill2.cost/100);bills1[i].q.pop();}printf("Total amount: $%.2f\n",(float)bills1[i].amount/100);}return 0;}


0 0
原创粉丝点击