[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: 稍微有點畏難,有點怕麻煩吧,還覺得自己的思路有點暴力,但是看了解答好像也是暴力的方式。然後以後要是沒思路的話先看思路然後自己寫寫看,實在不行再看解答。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章