PAT 1016 (sort)

1016 Phone Bills (25分)


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 he 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

 


First, item analysis

A long-distance telephone company charges according to the following rules: according to time spent on calls, long distance calls will cost a fee per minute. When a user starts a long distance call, time will be recorded, and when the user hangs up time will be recorded. Monthly monthly bill sent to the user (the rate is determined by the time of day). Your job is to prepare a monthly bill of the telephone call records.

Enter information:

  • Rate (24 non-negative integer): 0-1,1-2 ... 22-23,23-24
  • The number of records (N≤1000): each record for each test in the same month; off-line for the next recording, the recording is not paired with any online and offline chronological record with a user are recorded on-line will be ignored, not paired with online offline records will be ignored. Be sure to enter at least one call a good pairing. You may assume that no two records for the same customer the same time. Use 24-hour recording time.
  1. Username: 20 characters with no spaces
  2. Date and time: time:: month: day points
  3. Identification: on-line / off-line

Output: Dictionary order in accordance with the user's name

  •  first row:
  1. username
  2. Month bill (Example according to the output format)
  • The next few lines :( chronological order)
  1. Each call start time, end time (according to dd:hh:mm的格式)
  2. The total time duration (calculated in minutes)
  3. The call to spend much money
  • The last line :( embodiment in accordance with the output format)
  1. The total cost users month

Second, the problem-solving ideas:

We want to elect an effective call information for each user (in chronological order with a user on-line record for the next off-line record), so to all input records first sorted by user name dictionary order, then in accordance with the date and time sorting (date, hour, minute); after matching records sorted in the selected call log for each user; pairing time of calculating the number of minutes, and the amount of consumption .

  • Storage structure:
  1. It will be recorded as a structure: Username storage of records, start time, end time, signs
  2. 24-hour interval rate: storing the array
  • Enter by the rules:
  1. With a character input cin inconvenient and time-consuming a particular format, so here with scanf and the printf (COUT cin and preferably not mix)
  2. Use String char [] (not represented Scanf type String)
  • Sort by:
  1. sort(rec,rec+n,cmp);
  2. Rewrite rules specify the sort function cmp
  • Each record selected user pairing:
  1. Determines whether there is a valid call records: In needprint (= 0) as a sign (= 1 is encountered on, off on the basis of experience is = 2; not record the next time a valid user)
  2. There is further find a valid record matching records (in this case a user has identified a range of [on, next)):

  (2.1) value is determined on the scale: a match is found according to certain conditions on-line and off-line recording

  (2.2) determining the index value off: off +. 1 = ON (if off out of bounds then the record is no longer pairing)

  (2.3) calculation of the cost of this pairing record: the start time plus 1 continuously determines whether the expiration time arrives, and the rates for each minute spent sum (unit conversion cents -> US; two decimal places)

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

const int maxn=1010;
int toll[25]; //每小时区间内:每分钟多少钱
//结构体
struct Record{
    char name[25]; //姓名最多20个字符
    int month,dd,hh,mm;
    bool status; //true表示记录为online,否则为offline
}rec[maxn],temp;

//排序规则的制定
bool cmp(Record a,Record b){
    int s=strcmp(a.name,b.name);
    if(s!=0) //按名字字典顺序从小到大排序
        return s<0;
    else if(a.month!=b.month) //按月份从小到大排序
        return a.month<b.month;
    else if(a.dd!=b.dd) //按日期从小到大排序
        return a.dd<b.dd;
    else if(a.hh!=b.hh) //按小时从小到大排序
        return a.hh<b.hh;
    else //按分钟从小到大排序
        return a.mm<b.mm;
}

void get_ans(int on,int off,int &time,int &money){
    temp=rec[on];
    while(temp.dd<rec[off].dd||temp.hh<rec[off].hh||temp.mm<rec[off].mm){ //不断将起始时间加1,判断是否到达终止时间
        time++; //该次记录总时间加1min
        money+=toll[temp.hh]; //按照本小时区间的费率相加
        temp.mm++;
        if(temp.mm>=60){
            temp.mm=0;
            temp.hh++;
        }
        if(temp.hh>=24){
            temp.hh=0;
            temp.dd++;
        }
    }
}
int main()
{
    //数据的输入
    for(int i=0;i<24;i++){ //24小时每小时区间的资费
        scanf("%d",&toll[i]);
    }
    int n;
    scanf("%d",&n); //记录数
    char line[10]; //临时存放on-line和off-line的输入
    for(int i=0;i<n;i++){ //n条记录的具体信息
        scanf("%s",rec[i].name);
        scanf("%d:%d:%d:%d",&rec[i].month,&rec[i].dd,&rec[i].hh,&rec[i].mm);
        scanf("%s",line);
        if(strcmp(line,"on-line")==0){
            rec[i].status=true;
        }else{
            rec[i].status=false;
        }
    }

    //所有记录按规则排序
    sort(rec,rec+n,cmp);

    //查找并输出配对记录
    int on=0;
    int off;
    int next;
    while(on<n){
        //判断是否有配对、记录下一个用户编号next
        int needprint=0;
        next=on; //从当前位置开始寻找下一个用户
        while(next<n&&strcmp(rec[next].name,rec[on].name)==0){
            if(needprint==0&&rec[next].status==true){ //找到on
                needprint=1;
            }else if(needprint==1&&rec[next].status==false){ //找到on之后的第一个off
                needprint=2;
            }
            next++; //不断递增,直到找到下一个用户
        }
        if(needprint<2){ //该用户没有匹配的记录on-off
            on=next;
            continue; //继续找下一个用户
        }

        int allmoney=0;
        printf("%s %02d\n",rec[on].name,rec[on].month); //用户名和月份
        while(on<next){
            while(on<next-1&&!(rec[on].status==true&&rec[on+1].status==false)){
                on++;
            }
            off=on+1;
            if(off==next){ //所有配对已输出完毕,无配对
                on=next;
                break;
            }

            printf("%02d:%02d:%02d ",rec[on].dd,rec[on].hh,rec[on].mm);
            printf("%02d:%02d:%02d ",rec[off].dd,rec[off].hh,rec[off].mm);
            int time=0;
            int money=0;
            get_ans(on,off,time,money);
            allmoney+=money;
            printf("%d $%.2f\n",time,money/100.0);
            on=off+1;
        }
        printf("Total amount: $%.2f\n",allmoney/100.0);
    }

    return 0;
}

 

Published 30 original articles · won praise 2 · Views 8100

Guess you like

Origin blog.csdn.net/Ariel_x/article/details/104099028