PAT Advanced—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. 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-linerecord. 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,分开每个月份,根据时间排序就可以,最后再计算每条记录的费用,定义几个结构体来表示比较明确。

注意:

  1. 题目中说,For each test case, all dates will be within a single month,对于每个测试用例,最后将结果放在第一个月里,简化了要求!注意审题,不然会做很多无用功。
  2. Bills must be printed in alphabetical order of customers’ names.只需要根据用户名字的字母序排名即可
  3. 主要难点在于如何计费,可以利用模拟时间过程来计费,还可以利用差分来计费

反思:

  1. 做题之前先看清楚要求再做
  2. 这道题其实不必要搞那么多的结构体,由于只需要将所有月份的记录写到一个账单里,所以可以直接利用按排好序后的记录直接计算即可。
  3. 代码比较冗余,后面还要进行优化

代码:(C++)

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>

using namespace std;

int cost[24];

struct Phone
{
    int day;
    int hour;
    int min;
    string status;
};

struct Record
{
    //每人每月的通话记录
    map<int,vector<Phone> > rec;
};

struct Bill  //每条记录的花费
{
    Phone start;
    Phone end;
    int mins;  //总时间
    int chage; //花费
};

struct PerResult  //每个人每个月的花费
{
    string name;
    int month;
    vector<Bill> records;
    int amount;
};

vector<PerResult> result;  //用于输出的结果

map<string, Record> pers;  //保存每个用户的记录

bool cmp(Phone a, Phone b)
{
    //直接化为分钟来比较
    return (a.day*24*60 + a.hour*60 + a.min)<(b.day*24*60 + b.hour*60 + b.min);
}

bool cmp2(PerResult a, PerResult b)
{
    return a.name<b.name;
}

//模拟时间增长过程,计费
void countBill(Phone start, Phone end, int& time, int& money) {
    while(start.day < end.day || start.hour < end.hour || start.min < end.min) {
        time++;
        money += cost[start.hour];
        start.min++;
        if(start.min >= 60) {
            start.min = 0;
            start.hour++;
        }
        if(start.hour >= 24) {
            start.hour = 0;
            start.day++;
        }
    }
}

int main()
{
    int N;
    for(int i=0; i<24; i++)
        cin>>cost[i];
    cin>>N;
    for(int i=0; i<N; i++)
    {
        string name,status;
        int month;
        Phone ph;
        Record re;
        cin>>name;
        scanf("%d:%d:%d:%d", &month, &ph.day, &ph.hour, &ph.min);
        cin>>ph.status;

        pers[name].rec[month].push_back(ph);
    }
    for(map<string, Record >::iterator it=pers.begin(); it!=pers.end(); it++)
    {
        PerResult perR; //每个人的账单
        perR.name = it->first;  //姓名
        perR.amount = 0;
        bool flag = true;  //记录月份
        for(map<int, vector<Phone> >::iterator it2 = it->second.rec.begin(); it2!= it->second.rec.end(); it2++)
        {
            if(flag)
            {
                perR.month = it2->first;
                flag = false;
            }   
            vector<Phone> phs = it2->second; //该月的通话记录
            sort(phs.begin(), phs.end(), cmp);  //按时间排序
            for(int i=0; i<phs.size();)
            {
                //符合通话记录,直接+2
                if(phs[i].status == "on-line" && i+1<phs.size() && phs[i+1].status == "off-line")
                {
                    //计算花费及通话时间
                    Bill bill;
                    bill.start = phs[i];
                    bill.end = phs[i+1];
                    bill.chage = 0;
                    bill.mins = 0;
                    countBill(phs[i],phs[i+1], bill.mins,bill.chage);
                    perR.records.push_back(bill);
                    perR.amount += bill.chage;
                    i+=2;
                }
                else
                    i++; //不符合看下一条
            }
        }
        if(perR.records.size() != 0)  //可能有的人为空,账单为空,不能加进去
                result.push_back(perR);
    }
    sort(result.begin(),result.end(),cmp2);
    for(int i=0; i<result.size(); i++)
    {
        cout<<result[i].name;
        printf(" %02d\n", result[i].month);
        for(int j=0; j<result[i].records.size(); j++)
            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",result[i].records[j].start.day,result[i].records[j].start.hour,
                    result[i].records[j].start.min, result[i].records[j].end.day, result[i].records[j].end.hour,
                    result[i].records[j].end.min, result[i].records[j].mins, result[i].records[j].chage/100.0);
        printf("Total amount: $%.2lf\n", result[i].amount/100.0);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值