PAT 1016 Phone Bills (25 分)

13 篇文章 0 订阅

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了。又是一道臭长臭长的题。

题意可以理解为:计算每个人通话的时间和花费。

需要找到同一个用户按时间顺序匹配的通话记录(即同一用户距离最近的on-line和off-line)-->这一点排序后用map搞定。找不到匹配的记录就忽略掉。

很多细节需要注意, 需要注意的点是:

1、排序,先按照姓名的从小到大排序,如果名字相同,就按照通话的时间 从小到大 排序

2、每个人的通话记录 用 vector 存,用map映射,存储。这点很好理解

3、通话时间和花费的计算。因为每组的月份都相同,所以我们计算时间和花费的时候不需要考虑,只需要考虑 day,hour,minute即可。算通话开始时间和结束时间的时间和花费,可以选一个时间点为参照 比如 0:0:0 ,计算出开始时间和结束时间相对于这个时间的时间和花费,然后相减即可得到通话总时间和花费。

反正就是有一点点麻烦,代码是:

/*
通话 每分钟消费一个确定的价钱 ,取决于打电话的时间
任务是计算每月的花费,
*/
#include <bits/stdc++.h>
#define Max 1005
using namespace std;
typedef pair <int, int> pii;
int n;
int rate[30]; // 24小时 每个小时的收费
//消费记录
struct record{
    char name[21];
    int month, d, h, m; //时间
    int status;//状态
    bool operator < (const record & r) const{
        if(strcmp(name ,r.name) < 0) return true;
        if(strcmp(name ,r.name) == 0) {
            if(d < r.d) return true;
            if(d == r.d) {
                if(h < r.h) return true;
                if(h == r.h) {
                    if(m < r.m) return true;
                }
            } 
        }
        return false;
    }
}rec[Max];
//每个人通话记录的映射
map< string, vector<record> > mapp;

//得到时间和花费
pii get_TC(int d, int h, int m) {
    int tol_time = 0, tol_cost = 0, rt = 0;
    tol_time += d * 24 * 60 + h * 60 + m;
    /*-----花费计算-------*/
    for(int i = 0; i < 24; i++) rt += rate[i] * 60; //打一整天需要的钱
    tol_cost += d * rt + m * rate[h];
    for(int i = 0; i < h; i++) {
        tol_cost += rate[i] * 60;
    } 
    /*-------------------*/
    return make_pair(tol_time, tol_cost); 
}
void ac() {
    
    //把每个人的通话记录用vector记录起来。 然后用map映射
    for(int i = 0; i < n-1; i++){
        if(strcmp(rec[i].name,rec[i+1].name) == 0) {
            if(rec[i].status == 1 && rec[i+1].status == 0) {
                // cout << "name: "<< rec[i].name << endl;
                mapp[rec[i].name].push_back(rec[i]);
                mapp[rec[i].name].push_back(rec[i+1]);
            }
        }
    }

    // cout << "*******" << endl;

    //输出
    map<string, vector<record> > ::iterator it;
    for(it = mapp.begin(); it != mapp.end(); it++) {
        // cout << "!!!!!!!" << it -> first << endl;
        //个人的消费记录
        vector<record> rs = it -> second;
        cout << it -> first;
        printf(" %02d\n", rs[0].month);
        int tol_cost = 0, tol_time = 0;
        for(int i = 0; i < rs.size(); i+=2) {
            pii st = get_TC(rs[i].d, rs[i].h, rs[i].m);
            pii en = get_TC(rs[i+1].d, rs[i+1].h, rs[i+1].m);
            tol_time = en.first - st.first;
            // cost = (en.second - st.second)*1.0 / 100;
            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n", rs[i].d, rs[i].h, rs[i].m, rs[i+1].d, rs[i+1].h, rs[i+1].m, tol_time, (en.second - st.second)*1.0/100);
            tol_cost += en.second - st.second;
            // cout << " Cost " << tol_cost << endl;
        }
        // cout << " Cost!! " << tol_cost << endl;
        printf("Total amount: $%.2lf\n",tol_cost*1.0 / 100);
    }
}
int main() {
    // ios::sync_with_stdio (false);
    for(int i = 0; i < 24; i++) {
        scanf("%d",&rate[i]);
    }
    scanf("%d",&n);
    getchar();
    char sta[10];
    for(int i = 0; i < n; i++) {
        scanf("%s %d:%d:%d:%d %s",rec[i].name, &rec[i].month, &rec[i].d, &rec[i].h, &rec[i].m, sta);
        if(sta[1] == 'n'){
            rec[i].status = 1;
        }else rec[i].status = 0;
    }
    sort(rec, rec + n);
    // for(int i = 0; i < n; i++) {
    //     cout << "--> " << rec[i].name << "  " << rec[i].status << endl;
    // }
    ac();
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值