PAT 甲级 1016 Phone Bills

PAT 甲级 1016 Phone Bills

用结构体node保存一次call的人名、月、日、小时、分钟信息

因为要按人名输出,然后每个人名下按时间先后输出,所以我们需要给所有call进行排序,排序先按人名大小非降序排序,然后按时间先后排序

排序后,只要容器中前一个是on-line, 后一个是off-line,就是一次well paired

让人名为key,人名下的call的序列(排好序)为值,建立map

计算费用的方法

根据每个时间hh:mm:nn,计算从00:00:00到它的费用

某个时间段的费用 即 末时间点费用-初时间点费用

// 1016 Phone Bills.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

struct node {
    string name;
    int status, month, day, hour, minute, time;
};

bool cmp(node n1, node n2) {
    return n1.name != n2.name ? n1.name < n2.name : n1.time < n2.time;
}

double billfromzero(node call, int* rate) {
    double total = rate[call.hour] * call.minute + rate[24] * 60 * call.day;
    for (int i = 0; i < call.hour; i++) {
        total += rate[i] * 60;
    }
    total /= 100.0;
    return total;
}

int main()
{
    int rate[25] = { 0 }, n;
    for (int i = 0; i < 24; i++) {
        cin >> rate[i];
        rate[24] += rate[i];
    }
    cin >> n;
    vector<node> data(n);
    for (int i = 0; i < n; i++) {
        cin >> data[i].name;
        scanf("%d:%d:%d:%d", &data[i].month, &data[i].day, &data[i].hour, &data[i].minute);
        string temp;
        cin >> temp;
        data[i].status = (temp == "on-line") ? 1 : 0;
        data[i].time = data[i].day * 24 * 60 + data[i].hour * 60 + data[i].minute;
    }
    sort(data.begin(), data.end(), cmp);
    map<string, vector<node> > m;
    for (int i = 1; i < n; i++) {
        if (data[i].name == data[i - 1].name && data[i - 1].status == 1 && data[i].status == 0) {
            m[data[i - 1].name].push_back(data[i - 1]);
            m[data[i].name].push_back(data[i]);
        }
    }
    for (auto it : m) {
        vector<node> temp = it.second;
        cout << it.first;
        printf(" %02d\n", temp[0].month);
        double total = 0.;
        for (int i = 1; i < temp.size(); i+=2) {
            double t = billfromzero(temp[i], rate) - billfromzero(temp[i - 1], rate);
            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", temp[i - 1].day, temp[i - 1].hour, temp[i - 1].minute, temp[i].day, temp[i].hour, temp[i].minute, temp[i].time - temp[i - 1].time, t);
            total += t;
        }
        printf("Total amount: $%.2f\n", total);
    }
    return 0;
}

设置宽度和前置符号 “%02d” 宽度为2,前置0

 printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", temp[i - 1].day, temp[i - 1].hour, temp[i - 1].minute, temp[i].day, temp[i].hour, temp[i].minute, temp[i].time - temp[i - 1].time, t);

设置小数精度 “%.2f” 保留两位小数

 printf("Total amount: $%.2f\n", total);

print输出,参数不要加&,直接用变量名

scanf需要&,把值存到&指向的地址

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值