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

分析:

PAT甲级到目前为止AC最费力的题目(因为太菜了)。题目难度不大,主要考察结构体排序和时间格式处理。但是题目包含的信息量太大,每一点都要考虑全面真的很难。
四个测试点:
0~1:无有效通话记录的用户不能输出。(很多时候会忽略的)
2:on和off的时间在同一个小时但不在一天。
3:on和off时间既在同一天又在同一个小时。
思路:
将所有用户记录读入,每个用户都有一个vector存储其名下的所有记录。对这些记录按照时间升序排序(因为是mm:dd:hh的string格式,所以直接按照string排序即可)随后用stack进行这些有序记录的on-off匹配,一旦匹配成功的就存放到另一个存储有效记录pair<on,off>的vector.最后每个用户的有效记录输出,并计算通话时间和费用。
注意以下几个点:
1.题目保证至少有一条匹配得上的on-off,但不代表每一个人都有输出,即0、1测试点。
2.一旦有一个用户的on和off匹配成功,stack里面的所有on记录都需要清除。比方说lz在8:00和8:30接通电话,在8:35和17:00挂断电话,8:30和8:35组成一对匹配的on-off,但是lz只有一个,不可能在一个通话结束之后继续另一个当天更早时间的通话(细思极恐)
3.注意输出顺序按照名字字典序,所以lz采用map<string,int>建立名字和先后序号的映射,因为map的key是自动升序的,也可以用排序。
4.计算形如dd:hh:mm的时间差,直接dd1:hh1:mm1-dd:2hh2:mm2再转分钟即可。计算单次费用可以考虑00:00:00分别到on和off的费用,然后作差。

感觉代码写繁琐了,主要还是因为题目信息量大,一开始有很多地方想歪了,还是贴出来吧。。

源码:

#include<bits/stdc++.h>

#define Max 1000
using namespace std;
typedef struct phone {
    string name, t;
    int f;//f=1表示hang on
} record;//通话记录
vector<record> person[Max];//每个人的所有通话记录
vector<pair<string, string> > ans[Max];//每个人的真实记录,pair<string,string>记录一条有效通话
map<string, int> order;//映射人名和序号,人名字典序,序号按照输入顺序
int Index = 0;//通话记录中不同的人数
int charge[24];//每个时间段的收费
string mon;//月份(唯一确定的)

bool compare(record a, record b) {
    return a.t < b.t;
}

int main() {
    for (int i = 0; i < 24; i++)
        cin >> charge[i];
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
        char name[21], t[13], f[10];
        getchar();
        scanf("%s%s%s", name, t, f);
        record temp;
        if (!strcmp(f, "on-line"))
            temp.f = 1;
        else
            temp.f = 0;

        if (!order.count(name))//如果该通话者名字第一次出现,则Index++
            order[name] = Index++;

        temp.name = name;
        temp.t = t;
        mon = temp.t.substr(0, 2);//切片,提取出月份,因为所有记录都在一个月份所以重复赋值没有问题
        person[order[name]].push_back(temp);//此人帐下新添一条记录
    }

    for (int i = 0; i < Index; i++) {
        sort(person[i].begin(), person[i].end(), compare);
        stack<string> s;
        for (auto it = person[i].begin(); it != person[i].end(); it++) {
            if (it->f == 1)
                s.push(it->t);//on则压栈
            else {
                if (s.empty())continue;//栈空则该off记录无效
                ans[i].push_back(make_pair(s.top(), it->t));//匹配成功,在ans中新增一条有效通话
                while(!s.empty())//要注意一对on-off匹配上了,要把栈里所有hang on的记录清空
                    s.pop();
            }
        }
    }

    for (auto it = order.begin(); it != order.end(); it++) {
        string p = it->first;
        if (ans[it->second].size() == 0)continue;//此人没有有效通话记录
        else {
            double dollar = 0;//总费用
            printf("%s %s\n", p.c_str(), mon.c_str());//输出月份、姓名
            for (auto x = ans[it->second].begin(); x != ans[it->second].end(); x++) {
                string a = x->first;//on时间
                string b = x->second;//off时间
                int d1 = atoi(a.substr(3, 2).c_str());//天
                int d2 = atoi(b.substr(3, 2).c_str());
                int h1 = atoi(a.substr(6, 2).c_str());//时
                int h2 = atoi(b.substr(6, 2).c_str());
                int m1 = atoi(a.substr(9, 2).c_str());//分
                int m2 = atoi(b.substr(9, 2).c_str());
                for (int i = 3; i < a.length(); i++)
                    printf("%c", a[i]);
                printf(" ");
                for (int i = 3; i < b.length(); i++)
                    printf("%c", b[i]);
                printf(" ");
                //算时间差 单次费用
                int delta = (d2 - d1) * 24 * 60 + (h2 - h1) * 60 + m2 - m1;
                double price1 = d1 * accumulate(charge, charge + 24, 0) * 60 + accumulate(charge, charge + h1, 0) * 60 +
                                m1 * charge[h1];
                double price2 = d2 * accumulate(charge, charge + 24, 0) * 60 + accumulate(charge, charge + h2, 0) * 60 +
                                m2 * charge[h2];
                double price = (price2 - price1) / 100;
                printf("%d $%.2f\n", delta, price);
                dollar += price;
            }
            printf("Total amount: $%.2f\n", dollar);//总费用
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值