PAT A1016 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 (≤), 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <string>
#include <set>
#include <map>
using namespace std;
const int maxn = 1010;
int n;
string name,line;
int m, d, h, mm, all = 0;
int cost[24] = { 0 };
struct person {
    string name;
    int time;
    int d;
    int h;
    int mm;
    int status;
};
bool cmp(person p1, person p2) {
    return p1.name == p2.name ? p1.time < p2.time : p1.name < p2.name;
}
float cal_cost(person p) {
    float c = 0;
    c += p.d * all * 60;
    for (int j = 0; j < p.h; j++) {
        c += cost[j] * 60;
    }
    c += cost[p.h] * p.mm;
    return c;
}
vector<person> v;
map<string, vector<person> > mp;
int main() {
    for (int i = 0; i < 24; i++) {
        scanf("%d", &cost[i]);
        all += cost[i];
    }
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        cin >> name;
        scanf("%d:%d:%d:%d", &m, &d, &h, &mm);
        cin >> line;
        person p;
        p.name = name;
        if (line == "on-line") {
            p.status = 0;
        }
        else {
            p.status = 1;
        }
        p.d = d;
        p.h = h;
        p.mm = mm;
        p.time = 1440 * d + 60 * h + mm;
        v.push_back(p);
    }
    sort(v.begin(), v.end(), cmp);
    for (int i = 1; i < n; i++) {
        if (v[i].name == v[i - 1].name && (v[i].status == 1 && v[i - 1].status == 0)) {
            mp[v[i].name].push_back(v[i - 1]);
            mp[v[i].name].push_back(v[i]);
        }
    }
    for (auto it = mp.begin(); it != mp.end(); it++) {
        string name = it->first;
        cout << name;
        printf(" %02d\n", m);
        float total = 0;
        vector<person> pv = it->second;
        for (int i = 1; i < pv.size(); i += 2) {
            float cost_1 = 0, cost_2 = 0;
            cost_1 = cal_cost(pv[i - 1]);
            cost_2 = cal_cost(pv[i]);
            float total_cost = (cost_2 - cost_1) / 100;
            total += total_cost;
            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", pv[i - 1].d, pv[i - 1].h, pv[i - 1].mm, pv[i].d, pv[i].h, pv[i].mm, pv[i].time - pv[i - 1].time, total_cost);
        }
        printf("Total amount: $%.2f\n", total);
    }
    system("pause");
    return 0;
}

注意点:逻辑很简单的一道题,我却做了4个小时多才AC,实在太菜了。

一开始没想到这道题本质是个排序题,看到要按从小到大排列,就想到了set和map自动排序,然后就进去出不来了,一直纠结在怎么判断两个通话记录是一对上。

第二个坑是计算花费,看了大佬的思路才发现原来可以通过相对值的差来得到,而不一定非要把两个时间的差求出来,把每个时间点相对0的价格算出来以后的差就是所要的价格。

第三点是最后输出结果,对每个人还要输出一个总和,自己只想到了用各种if判断,看了大佬思路,用map的vector实在是太方便了,而且还准确。

原文地址:https://www.cnblogs.com/tccbj/p/10387731.html

时间: 2024-10-11 23:13:10

PAT A1016 Phone Bills (25 分)的相关文章

PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

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 connec

PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequence

A1016. 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,

PAT 5-8 File Transfer (25分)

We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other? Input Specification:

PAT 07-图6 旅游规划 (25分)

有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条路径都是最短的,那么需要输出最便宜的一条路径. 输入格式: 输入说明:输入数据的第1行给出4个正整数NN.MM.SS.DD,其中NN(2\le N\le 5002≤N≤500)是城市的个数,顺便假设城市的编号为0~(N-1N−1):MM是高速公路的条数:SS是出发地的城市编号:DD是目的地的城市编号.随后的MM行中,每行给出一条高

PAT B1045 快速排序 (25 分)

著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边. 给定划分后的 N 个互不相同的正整数的排列,请问有多少个元素可能是划分前选取的主元? 例如给定 $N = 5$, 排列是1.3.2.4.5.则: 1 的左边没有元素,右边的元素都比它大,所以它可能是主元: 尽管 3 的左边元素都比它小,但其右边的 2 比它小,所以它不能是主元: 尽管 2 的右边元素都比它大,但其左边的 3 比它大,所以它不能是主

PAT Basic 1055 集体照 (25 分)

拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每排人数为 /(向下取整),多出来的人全部站在最后一排: 后排所有人的个子都不比前排任何人矮: 每排中最高者站中间(中间位置为 /,其中 m 为该排人数,除法向下取整): 每排其他人以中间人为轴,按身高非增序,先右后左交替入队站在中间人的两侧(例如5人身高为190.188.186.175.170,则队形为175.188.190.186.170.这里假设你面对拍照者,所以你的左边是中间人的右边): 若多人身高相同,则按名

PAT Basic 1045 快速排序 (25 分)

著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边. 给定划分后的 N 个互不相同的正整数的排列,请问有多少个元素可能是划分前选取的主元? 例如给定 $N = 5$, 排列是1.3.2.4.5.则: 1 的左边没有元素,右边的元素都比它大,所以它可能是主元: 尽管 3 的左边元素都比它小,但其右边的 2 比它小,所以它不能是主元: 尽管 2 的右边元素都比它大,但其左边的 3 比它大,所以它不能是主

PAT Advanced 1029 Median (25分)

Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequences is defined to be