[code] PTA 胡凡算法笔记 DAY018

题目 A1016 Phone Bills

在这里插入图片描述

  • 题意
    给出一天24小时每个小时的分钟计费,然后给出用户及其通话记录,判断其中有效的记录,计算产生费用,最终采用名字升序的顺序给出用户的月账单。

  • 思路
    主要要实现三个模块:
    ① 将按题意记录排序(name, day, hour, minute递增)
    ② 获取其中的有效记录(采用needPrint去记录,遇到on = 1, 下一个再遇到off = 2 即为有效记录)
    ③ 计算分钟数及话费 (因为计费是按分钟收费,所以以每分钟的增幅去向off靠拢即可)
    注意: on和off相邻才算是有效的一条通话记录,即x[i] == "on-line" && x[i-1] == "off-line"

  • Code in C++

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#define maxn 1001

struct record {
    std::string name;
    int month;
    int day;
    int hour;
    int minute;
    std::string word;
    void print() {
        std::cout << name << " " << month << ":" << day << ":" << hour << ":" << minute << " " << word << std::endl;
    }
} records[maxn], tmp;

bool cmp(const record &a, const record &b) {
    if (a.name != b.name) return a.name < b.name;
    else if (a.month != b.month) return a.month < b.month;
    else if (a.day != b.day) return a.day < b.day;
    else if (a.hour != b.hour) return a.hour < b.hour;
    else if (a.minute != b.minute) return a.minute < b.minute;
}

int toll[24] = {0};

void get_ans(int on, int off, int &time, int &money) {
    tmp = records[on];
    while (tmp.day < records[off].day || tmp.hour < records[off].hour || tmp.minute < records[off].minute) {
        ++time;
        money += toll[tmp.hour];
        ++tmp.minute;
        if (tmp.minute == 60) {
            tmp.minute = 0;
            tmp.hour++;
        }
        if (tmp.hour == 24) {
            tmp.hour = 0;
            tmp.day++;
        }
    }
}

int main()
{
    for (int i = 0; i < 24; ++i) {
        scanf("%d", &toll[i]);
    }

    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        std::cin >> records[i].name;
        scanf("%d:%d:%d:%d", &records[i].month, &records[i].day, &records[i].hour, &records[i].minute);
        std::cin >> records[i].word;
    }
    std::sort(records, records + n, cmp);
    // 筛选有效的
    int on = 0, off, next; // on和off是配对的一条,next为下一个用户
    while (on < n) {
        int needPrint = 0; // 记录是否找到有效记录 2为找到
        next = on;         // 从on开始扫描
        while (next < n && records[next].name == records[on].name) {
            if (needPrint == 0 && records[next].word == "on-line") {
                needPrint = 1;
            } else if (needPrint == 1 && records[next].word == "off-line") {
                needPrint = 2;
            }
            ++next;
        }

        if (needPrint < 2) {
            on = next;
            continue;
        }

        int allMoney = 0;
        printf("%s %02d\n", records[on].name.c_str(), records[on].month);
        while (on < next) {
            while (on < next -1 && !(records[on].word == "on-line" && records[on + 1].word == "off-line")) {
                ++on;
            }
            off = on + 1;
            if (off == next) {
                on = next;
                break;
            }
            printf("%02d:%02d:%02d ", records[on].day, records[on].hour, records[on].minute);
            printf("%02d:%02d:%02d ", records[off].day, records[off].hour, records[off].minute);
            int time = 0, money = 0;
            get_ans(on, off, time, money);
            allMoney += money;
            printf("%d $%.2f\n", time, money / 100.0); // cents -> dollar
            on = off + 1;
        }
        printf("Total amount: $%.2f\n", allMoney / 100.0);
    }
    return 0;
}


小结

  1. 先伪码大概思路模块化,然后再一个模块一个模块的去攻克。
  2. 1 dollar = 100 cents

PS: 稍微有点畏难,有点怕麻烦吧,还觉得自己的思路有点暴力,但是看了解答好像也是暴力的方式。然后以后要是没思路的话先看思路然后自己写写看,实在不行再看解答。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值