PTA 1016 Phone Bills

int tolls[24];
int N;
struct Record
{
    string name, time, type;
};

// 计算分钟数和费用
double get_toll(string on_time, string off_time, int &cost_time)
{
    int on_day, on_hour, on_min, off_day, off_hour, off_min;
    on_day = stoi(on_time.substr(3, 2));
    on_hour = stoi(on_time.substr(6, 2));
    on_min = stoi(on_time.substr(9, 2));
    
    off_day = stoi(off_time.substr(3, 2));
    off_hour = stoi(off_time.substr(6, 2));
    off_min = stoi(off_time.substr(9, 2));

    double toll = 0;
    
    while (on_day != off_day || on_hour != off_hour || on_min != off_min) {
        toll += tolls[on_hour]; 
        cost_time++;
        on_min++;

        if (on_min == 60) {
            on_min = 0;
            on_hour++;
        }
        if (on_hour == 24) {
            on_hour = 0;
            on_day++;
        }
    }
    return toll / 100.0;
}

int main()
{
    for(int i = 0; i < 24; ++i)
        cin >> tolls[i];
    cin >> N;
    
    string month;

    // map存储各用户的通话记录,名字按照字母序
    map<string, vector<Record>> calls;
    
    for(int i = 0; i < N; ++i)
    {
        Record record;
        cin >> record.name >> record.time >> record.type;
        month = record.time.substr(0, 2);
        calls[record.name].push_back(record);
    }

    // 处理每个用户的通话记录:匹配 + 计算
    for(auto &user : calls)
    {
        string name = user.first;
        vector<Record> &logs = user.second;
        
        double sum_toll = 0;

        // 该用户通话记录按时间排序
        sort(logs.begin(), logs.end(), [](const Record &r1, 
                                          const Record &r2){
                 return r1.time < r2.time;
             });

        // 匹配有效的通话记录
        vector<pair<string, string>> lines;
        for(size_t i = 0; i < logs.size() - 1; ++i)
        {
            if(logs[i].type == "on-line" && logs[i + 1].type == "off-line")
                lines.push_back({logs[i].time, logs[i + 1].time});
        }

        // 无有效通话记录
       if(lines.empty())
           continue;

        // 可能某个用户没有有效的通话记录,这行打印信息不能放在前面
        cout << name << " " << month << endl;

        // 计算分钟数和费用并打印信息
        for(auto [on_time, off_time] : lines)
        {
            
            int cost_time = 0;
            double toll = get_toll(on_time, off_time, cost_time);

            cout << on_time.substr(3) << " " << off_time.substr(3) << " " << cost_time;
            printf(" $%.2f\n", toll);
            sum_toll += toll;
        }

        printf("Total amount: $%.2f\n", sum_toll);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值