PAT_A 1016 Phone Bills

PAT_A 1016 Phone Bills

分析

本题属实比较的麻烦。

  • 题目要求生成每个人对应的通话账单。
  • 输入包括每天24小时中每小时的话费表;
  • N条接通与挂断的通话记录,只有存在一对接通挂断时,才认为是有效的通话记录。
  • 为此首先设定时间类,人物类。并将输入的记录分别划归到指定人名下。
  • 对各人物按照人名递增排序,接着对其通话记录按时间递增排序,且输入保证所有记录均在一个月内;
  • 下一步对通话记录进行整理,由于题目要求只保留在时间上相邻的on-lineoff-line,可据此更新每人的有效记录个数。
  • 最后计算通话的时间以及费用,这里时间相减采用模拟的方式(居然没超时),重载的减号-返回值为长度为24小时的整型数组,用于计算费用与时长;就可以按照指定格式输出了。
  • 要注意若没有有效的一对通话记录,是不输出对应人的账单的。

题目的描述

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

AC的代码

#include<bits/stdc++.h>

using namespace std;

class times{
    public:
    int MM,dd,HH,mm;
    bool of;//1 on; 0 off
    times(){}
    times(int M,int d,int H,int m,bool ofl){
        MM=M;
        dd=d;
        HH = H;
        mm = m;
        of =ofl;
    }
    array<int , 24> operator -(times t){
        array<int,24> res = {0};
        while(dd!=t.dd||HH!=t.HH||mm!=t.mm){
            t.mm++;
            res[t.HH]++;
            if(t.mm==60){
                t.mm=0;
                t.HH+=1;
            }
            if(t.HH==24){
                t.dd++;
                t.HH=0;
            }
        }
        return res;
    }
};
bool cmp1(times a,times b){
    if(a.dd!=b.dd){
        return a.dd < b.dd;
    }
    else if(a.HH!=b.HH){
        return a.HH<b.HH;
    }
    else if(a.mm!=b.mm){
        return a.mm<b.mm;
    }
}
class person{
    public:
    array<times, 1001> timeslist;
    int tl;
    long long ttt;
    string names;
    person(){}
    person(string n){
        names = n;
        tl=0;
        ttt=0;
    }
    void filter(){
        sort(timeslist.begin(),timeslist.begin()+tl,cmp1);
        int i=0,j=0;
        while(j+1<tl){
            if(timeslist[j].of&&!timeslist[j+1].of){
                timeslist[i]=timeslist[j];
                timeslist[i+1]=timeslist[j+1];
                i+=2;
                j+=2;
            }
            else j++;
        }
        tl = i;
    }
};

bool cmp2(person a,person b){
    return a.names<b.names;
}
array<person,1001> people;
int main(){
    array<int,24> triffs;
    for(int i=0;i<24;i++){
        scanf("%d",&triffs[i]);
    }
    int N;
    scanf("%d",&N);
    
    int pl = 0;
    while(N--){
        bool f = false,off;
        int MM,dd,hh,mm;
        char tn[22],ofl[22];
        scanf("%s%d:%d:%d:%d%s",tn,&MM,&dd,&hh,&mm,ofl);
        if("on-line"==string(ofl)){
            off = true;
        }
        else off =false;
        for(int i=0;i<pl;i++){
            if(people[i].names==string(tn)){
                people[i].timeslist[people[i].tl++] = times(MM,dd,hh,mm,off);
                f=true;
                break;
            }
        }
        if(!f){
            people[pl] = person(string(tn));
            people[pl].timeslist[people[pl].tl++] = times(MM,dd,hh,mm,off); 
            pl++;
        }
    }
    sort(people.begin(),people.begin()+pl,cmp2);
    for(int i=0;i<pl;i++){
        people[i].filter();
//         printf("%s ",people[i].names);
        if(people[i].tl<2){
            continue;
        }
        cout<<people[i].names+' ';
        printf("%02d\n",people[0].timeslist[0].MM);
        double tot = 0,tothh=0;
        long long tottimes=0;
        array<int ,24> tothhlists;
        for(int j=1;j<people[i].tl;j+=2){
            tothh=0;
            tottimes=0;
            tothhlists = people[i].timeslist[j]-people[i].timeslist[j-1];
            for(int k=0;k<24;k++){
                tothh+=triffs[k]*tothhlists[k];
                tottimes+=tothhlists[k];
            }
            printf("%02d:%02d:%02d %02d:%02d:%02d %lld $%.2lf\n",
                   people[i].timeslist[j-1].dd,people[i].timeslist[j-1].HH,people[i].timeslist[j-1].mm,
                   people[i].timeslist[j].dd,people[i].timeslist[j].HH,people[i].timeslist[j].mm,
                   tottimes,tothh/100
                  );
            tot+=tothh;
        }
        printf("Total amount: $%.2lf\n",tot/100);
    }
    return 0;
}
posted @ 2022-01-24 22:59  ghosteq  阅读(27)  评论(0编辑  收藏  举报