PAT 1016. Phone Bills (25)

21 篇文章 0 订阅
8 篇文章 0 订阅

PAT 1016. Phone Bills (25)

题目分析

原题相信大家都看见了,题目几乎不涉及算法,主要就是考大家的细心,题目大意就是给我们所有用户的长途电话记录,根据用户分组,计算每个用户需要交的电话费,打印账单,这个题隐含着如果一个用户没有一次完整的打电话记录就不输出,我起初在这个上面栽了跟头,后来及时更正了。

解题逻辑

  1. 读入花费的收费信息
  2. 读入用户的信息,使用map将不同的用户信息分隔开,将on-lineoff-line记录放到用户的记录表中,标记记录类型。
  3. map进行遍历,对用户的on-lineoff-line记录排序,找出所有可以组成完整通话的记录,计算费用并记录。
  4. 输出信息

代码

#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int rate[24];

struct Record {
    int day, hour, min;
    char type;
    Record(int day, int hour, int min, char ch) :day(day), hour(hour), min(min), type(ch) {}
    // 便于排序
    friend bool operator<(const Record &a, const Record &b) {
        return a.day * 1440 + a.hour * 60 + a.min < b.day * 1440 + b.hour * 60 + b.min;
    }
    // 便于输出分钟差
    friend int operator-(const Record &a, const Record &b) {
        return a.day * 1440 + a.hour * 60 + a.min - b.day * 1440 - b.hour * 60 - b.min;
    }
};

struct Bill {
    int start;
    int end;
    double cost;
};

struct User {
    string name;
    vector<Record> recs;
    friend bool operator<(const User &a, const User &b) {
        return a.name < b.name;
    }
};

double calcu_cost(const Record &start, const Record &end) {
    // 判断是否跨天了
    double cost = 0;
    if (start.day == end.day) {
        // 没有跨天
        int hour = start.hour;
        // 开始没有考虑时间在同一个小时,被坑好大会儿
        if (start.hour == end.hour) {
            cost += rate[hour] * (end.min - start.min);
        }
        else {
            cost += rate[hour] * (60 - start.min);
            ++hour;
            while (hour < end.hour) {
                cost += rate[hour] * 60;
                hour++;
            }
            cost += rate[end.hour] * end.min;
        }
    }
    else {
        // 跨天了,问题是有可能跨好几天。。。
        int cross_days = end.day - start.day - 1;
        for (int i = 0; i < 24; ++i) {
            cost += rate[i] * 60 * cross_days;
        }
        // 计算只跨一次00:00的费用
        int hour = start.hour;
        int end_hour = end.hour + 24;
        cost += rate[hour] * (60 - start.min);
        hour++;
        while (hour < end_hour) {
            cost += rate[hour % 24] * 60;
            ++hour;
        }
        cost += rate[end.hour] * end.min;
    }
    return cost / 100;
}

int main() {
    for (int i = 0; i < 24; ++i) {
        scanf("%d", &rate[i]);
    }
    int cnt;
    scanf("%d", &cnt);
    char name[30];
    char on_off[20];
    string s;
    int month, day, hour, min;
    map<string, User>m;

    while (cnt--) {
        scanf("%s %d:%d:%d:%d %s", name, &month, &day, &hour, &min, on_off);
        s = name;
        m[s].recs.emplace_back(day, hour, min, on_off[1]);
    }
    // 开始算帐单,一个一个用户的输出
    for (auto &u : m) {
        vector<Bill> bills;
        Bill b;
        bool good = false;
        double total = 0;
        double once = 0;
        auto &rs = u.second.recs;
        // 把on和off先排序
        sort(rs.begin(), rs.end());
        int size = rs.size();
        for (int i = 0; i < size - 1; ++i) {
            if (rs[i].type == 'n' && rs[i + 1].type == 'f') {
                once = calcu_cost(rs[i], rs[i + 1]);
                b.start = i;
                b.end = i + 1;
                b.cost = once;
                total += once;
                bills.push_back(b);
                good = true;
            }
        }

        if (good) {
            printf("%s %02d\n", u.first.c_str(), month);
            int start, end;
            double cost;
            for (auto &bill : bills) {
                start = bill.start;
                end = bill.end;
                cost = bill.cost;
                printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", rs[start].day, rs[start].hour, 
                    rs[start].min, rs[end].day, rs[end].hour, rs[end].min, rs[end] - rs[start],
                    cost);
            }
            printf("Total amount: $%.2f\n", total);
        }
    }
    return 0;
}

注意事项

计算花费很容易出错,记得细心。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值