【PAT甲级】1016 Phone Bills

本文介绍了一种算法,用于计算长途电话费用并按客户名称和通话时间排序。通过解析24小时费率结构和通话记录,实现了有效通话记录的筛选、时间和费用计算。文章详细解释了关键步骤,如时间差计算、费用累积,并提供了完整代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,随后附上n条通话记录,每条通话记录内容有顾客姓名,记录时间,记录类型,记录类型有上线记录和下线记录。

题目的输出要求给出每个顾客的有效通话记录,每条通话记录的开始和结束时间,每条通话记录的单独话费和最后的总花费。

个人思路

这题其实还是对排序算法的应用,有几个关键点如下所示。

1、要对通话记录进行排序,要实现先按字母序排序,后按通话时间从小到大排序,因此要自己写出一个比较函数。

2、要计算出每条通话记录从上线到下线的时间段内的总话费,每个小时的每分钟话费可能都不同。我是在一个循环中模拟时间的增长,每个小时单独计算增加的话费,同时将开始时间增加,直到两个时间相等时退出循环。

3、要判断哪些通话记录是有效的,要注意:如果上条记录是上线的,下条记录如果是上线则替换上条记录的上线时间,下条记录如果是下线的则进行话费计算;如果上条记录是下线的,下条记录如果是下线的则直接忽略,下条记录如果是上线的则开始新的一条记录。

4、本题最大的坑:输出时只输出话费大于0的顾客,如果话费为0,连名字都不输出。

代码实现

#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
#define ll long long
#define eps 1e-8
#define INF 0x7FFFFFFF

using namespace std;

// 24小时话费
int cents_per_min[24] = {0};

// 时间结构体
struct CallTime {
    int month, day, hour, minute;
};

// 通话记录结构体
struct CallRecord {
    string name;
    CallTime call_time;
    bool online; // 记录类型 1上线 0下线
};

bool cmp(CallRecord r1, CallRecord r2) {
    if (r1.name == r2.name) {
        // return r1.call_time < r2.call_time
        if (r1.call_time.day < r2.call_time.day) return true;
        else if(r1.call_time.day > r2.call_time.day)return false;
        
        if (r1.call_time.hour < r2.call_time.hour) return true;
        else if(r1.call_time.hour > r2.call_time.hour) return false;
        
        if (r1.call_time.minute < r2.call_time.minute) return true;
        else if(r1.call_time.minute > r2.call_time.minute)return false;
    }
    return r1.name < r2.name;
}

// 求出两个时间的分钟差
int time_sub(CallTime t1, CallTime t2) {
    int min1, min2;
    min1 = t1.day*24*60 + t1.hour*60 + t1.minute;
    min2 = t2.day*24*60 + t2.hour*60 + t2.minute;
    return abs(min1-min2);
}

// 求出某个电话记录的话费
int phone_cost(CallTime t1, CallTime t2) {
    int ret = 0;
    while (!(t1.day == t2.day && t1.hour == t2.hour && t1.minute == t2.minute)) {
        if (t1.day == t2.day && t1.hour == t2.hour) {
            ret += cents_per_min[t1.hour]*(t2.minute-t1.minute);
            t1.minute = t2.minute;
        }
        else {
            ret += cents_per_min[t1.hour]*(60-t1.minute);
            t1.minute = 0;
            if (t1.hour == 23) {
                t1.hour = 0;
                t1.day ++;
            }
            else {
                t1.hour ++;
            }
        }
    }
    return ret;
}

int main() {
    // 输入cents per minute
    for (int i = 0; i < 24; i ++) {
        cin >> cents_per_min[i];
    }
    // 输入记录个数
    int n;
    cin >> n;
    // 对每条记录进行存储
    vector<CallRecord> records;
    for (int i = 0; i < n; i ++) {
        // 将输入信息先存放到record里
        CallRecord record;
        string name, call_time, type;
        cin >> name >> call_time >> type;
        record.name = name;
        const char *s = call_time.data();
        sscanf(s, "%d:%d:%d:%d", &record.call_time.month, &record.call_time.day, &record.call_time.hour, &record.call_time.minute);
        if (type == "on-line") record.online = true;
        else record.online = false;
        // 将记录存入vector
        records.push_back(record);
    }
    
    // 按照先字母表,后时间前后进行排序
    sort(records.begin(), records.end(), cmp);
    
    int sum = 0; // 记录总开销
    bool last_online = false; // 记录上一条记录是否是online
    CallTime begin_time, end_time; // 一条通话记录的开始和结束时间
    map <string, int> idx; // 建立顾客姓名和姓名编号的映射
    int name_cnt = 0; // 顾客姓名数量
    for (int i = 0; i < n; i ++) {
        CallRecord record = records[i];
        // 如果是上线记录,则记录开始电话的时间
        if (record.online) {
            begin_time = record.call_time;
            last_online = true;
        }
        // 如果是下线记录,且上条记录是上线记录
        else if (last_online && !record.online) {
            end_time = record.call_time;
            last_online = false;
            // 计算分钟数和花费数
            int minutes = time_sub(begin_time, end_time);
            int rec_cost = phone_cost(begin_time, end_time);
            sum += rec_cost;
            // 第一次时打印名字
            if(idx[record.name] == 0) {
                idx[record.name] = ++name_cnt;
                cout << record.name << " ";
                printf("%02d\n", record.call_time.month);
            }
            // 打印通话记录
            printf("%02d:%02d:%02d ", begin_time.day, begin_time.hour, begin_time.minute);
            printf("%02d:%02d:%02d ",end_time.day, end_time.hour, end_time.minute);
            printf("%d $%.2lf\n", minutes, 1.0*rec_cost/100);
        }
        
        bool customer_end = false;
        if (i == n-1) customer_end = true;
        else if (record.name != records[i+1].name) customer_end = true;
        if (customer_end) {
            if (sum != 0) printf("Total amount: $%.2lf\n", 1.0*sum/100);
            sum = 0;
            last_online = false;
        }
        
    }
    return 0;
}

总结

学习不息,继续加油

PAT奇奇怪怪的坑实在是太多了,审题真的很重要。

### PAT 甲级 真题 1172 解析 对于PAT甲级真题1172,该题目名为“Phone Bill”,主要考察字符串处理以及简单的数据结构应用能力。此题目的背景设定为客户通话记录统计问题。 #### 题目描述 给定一组电话号码及其对应的拨打时间和持续时间,计算每位用户的月账单总额。每条通话记录包含三个字段:电话号码、起始时间和结束时间。要求按照输入顺序输出每个客户的总费用,并保留两位小数[^1]。 #### 输入格式说明 - 第一行给出正整数N (≤10^5),表示有N次呼叫; - 接下来N行,每行提供一次呼叫的信息:“手机号码 起始时刻 结束时刻”。其中,“起始时刻”和“结束时刻”的格式均为HH:MM:SS; #### 输出格式说明 - 对于每一个客户,先打印其手机号码,再跟上冒号和空格,最后是当月话费金额(精确到分),单位为元人民币RMB。 #### 示例代码实现 ```cpp #include <iostream> #include <map> #include <iomanip> using namespace std; int main() { int n; cin >> n; map<string, double> bills; while(n--) { string number; char start_time[9], end_time[9]; scanf("%s %s %s", &number[0], start_time, end_time); // Convert time strings to seconds since midnight. sscanf(start_time, "%*d:%*d:%d", &start_seconds); sscanf(end_time, "%*d:%*d:%d", &end_seconds); // Calculate duration and update bill accordingly. int duration = end_seconds - start_seconds; if(duration > 0){ bills[number] += ceil((double)duration / 60 * 0.01); } } for(auto& entry : bills){ cout << entry.first << ": " << fixed << setprecision(2) << entry.second << endl; } return 0; } ``` 上述C++程序实现了对输入数据的读取与处理逻辑,通过`<map>`容器来存储并累加各个用户的通话时长及相应费用。需要注意的是,在实际比赛中应当更加严谨地验证输入的有效性和边界条件。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值