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

算法描述:排序 哈希表

题目大意:

首先依次给出24小时不同时间段的计费价格
然后给出n条不同用户电话上线/下线记录
按照常识给每个用户计算单次时长 和 费用 以及 该用户的总费用

#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
// 题目不难   但是特别繁琐(也可能是我太菜了)
int n;
struct records
{
    int dd, hh, mm, t; //t记录以分为单位的总时长
    string tag;
    
    bool operator< (const records& q)const
    {
        return t < q.t;
    }
}rec[1010];

int price[25]; //不同时段单价
map<string, vector<records>> M; // 姓名对应记录信息

int main()
{
    for(int i = 0 ; i < 24 ; i ++)  cin >> price[i];
    // 处理输入  没什么好说的
    cin >> n;
    int month;
    for(int i = 0 ; i < n ; i ++)
    {
        string name, tag;
        char c;
        int d, h, m;
        cin >> name >> month >> c >> d >> c >> h >> c >> m >> tag;
        records temp;
        temp.dd = d;
        temp.hh = h; 
        temp.mm = m;
        temp.t = d * 1440 + h * 60 + m;
        temp.tag = tag;
        M[name].push_back(temp);
    }
// 依次遍历map容器  每次遍历即是一个人的通话记录
    for(auto it = M.begin() ; it != M.end() ; it ++)
    {
        auto y = it->second;
        sort(y.begin(), y.end());
        double tot_cost = 0;
	// 遍历map(每个人)的上/下线记录进行匹配
        for(int i = 0 ; i < y.size() ;)
        {
            if(i + 1 < y.size() && y[i].tag > y[i + 1].tag) //  配对成功 即on 在 off前面  on > off
            {
                if(tot_cost == 0) //第一次配对成功
                {
                    cout << it->first;
                    printf(" %02d\n", month);
                }
                int t1 = y[i].t;
                int t2 = y[i + 1].t;
                double cost = 0;
                for(int time = t1 ; time < t2 ; time ++)
                    cost += price[time % 1440 / 60]; // 当前时间的时区
                printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", y[i].dd, y[i].hh, y[i].mm, y[i+1].dd, y[i+1].hh, y[i+1].mm, y[i+1].t-y[i].t, cost / 100);
                i += 2;
                tot_cost += cost;
            }
            else
                i ++;
        }
        if(tot_cost)
            printf("Total amount: $%.2f\n", tot_cost / 100);
    }
    return 0;
}
posted @ 2022-08-12 22:40  D_coding_blog  阅读(38)  评论(0)    收藏  举报