⬆︎
×

[PAT-A] 1016 Phone Bills

Hyplus目录

Java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;

/**
 * 2个测试点超时
 */
public class Main {
    static int totalFee = 0;
    static int[] fee = new int[24];

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] fee1 = reader.readLine().split(" ");
        for (int i = 0; i < fee.length; i++) {
            fee[i] = Integer.parseInt(fee1[i]);
            totalFee += fee[i];
        }

        int n = Integer.parseInt(reader.readLine());
        Map<String, ArrayList<Record>> map = new TreeMap<>(Comparator.naturalOrder());

        for (int i = 0; i < n; i++) {
            String[] str = reader.readLine().split(" ");
            Record record = new Record(str[0], str[1], str[2]);
            if (!map.containsKey(str[0]))
                map.put(str[0], new ArrayList<>());
            map.get(str[0]).add(record);
        }

        for (var entry : map.entrySet()) {
            ArrayList<Record> list = entry.getValue();
            Collections.sort(list);
            double singleAmount = 0;
            double totalAmount = 0;
            Record lastRecord = list.get(0);
            StringBuilder output = new StringBuilder(String.format("%s %02d\n", entry.getKey(), lastRecord.month));

            for (Record currentRecord : list) {
                if (currentRecord.status.equals("on-line")) {
                    lastRecord = currentRecord;
                    continue;
                } else {
                    if (lastRecord.status.equals("on-line")) {
                        int minute = currentRecord.relativeTime - lastRecord.relativeTime;
                        singleAmount = (currentRecord.relativeFee - lastRecord.relativeFee) / 100.0;
                        totalAmount += singleAmount;
                        output.append(String.format("%s %s %d $%.2f\n",
                                lastRecord.time.substring(3), currentRecord.time.substring(3), minute, singleAmount));
                    }
                }
                lastRecord = currentRecord;
            }
            output.append(String.format("Total amount: $%.2f", totalAmount));
            if (totalAmount > 0)
                System.out.println(output);
        }

    }

    static class Record implements Comparable<Record> {
        String id;
        String time;
        int month;
        int day;
        int hour;
        int minute;
        String status;
        double relativeFee;
        int relativeTime;

        public Record(String id, String time, String status) {
            this.id = id;
            this.time = time;
            String[] str = time.split(":");
            this.month = Integer.parseInt(str[0]);
            this.day = Integer.parseInt(str[1]);
            this.hour = Integer.parseInt(str[2]);
            this.minute = Integer.parseInt(str[3]);
            this.status = status;
            this.relativeTime = day * 1440 + hour * 60 + minute;
            this.relativeFee += minute * fee[hour];
            for (int i = 0; i < hour; i++)
                this.relativeFee += fee[i] * 60;
            this.relativeFee += day * 60 * totalFee;
        }

        @Override
        public int compareTo(Record r1) {
            return this.relativeTime - r1.relativeTime;
        }
    }
}

C++

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

const int N = 24 * 60 * 31 + 10;

int n;
int cost[24];
double sum[N];

struct Record {
    int time;
    string state;
    int month;
    string format_time;

    bool operator<(const Record &t) const {
        return time < t.time;
    }
};

map <string, vector<Record>> persons;

int main() {
    for (int i = 0; i < 24; ++i)
        cin >> cost[i];
    for (int i = 1; i < N; ++i)
        sum[i] = sum[i - 1] + cost[(i - 1) % 1440 / 60] / 100.0;

    cin >> n;
    char name[25], state[25], format_time[25];
    for (int i = 0; i < n; ++i) {
        int month, day, hour, minute;
        scanf("%s %d:%d:%d:%d %s", name, &month, &day, &hour, &minute, state);

        sprintf(format_time, "%02d:%02d:%02d", day, hour, minute);
        int time = (day - 1) * 24 * 60 + hour * 60 + minute;
        persons[name].push_back({time, state, month, format_time});
    }

    for (auto &person: persons) {
        auto records = person.second;
        sort(records.begin(), records.end());
        double total = 0;

        for (int i = 0; i < records.size() - 1; ++i)
            if (records[i].state == "on-line" && records[i + 1].state == "off-line") {
                if (total == 0) printf("%s %02d\n", person.first.c_str(), records[i].month);
                cout << records[i].format_time << " " << records[i + 1].format_time << " ";

                int gap = records[i + 1].time - records[i].time;
                double money = sum[records[i + 1].time] - sum[records[i].time];
                printf("%d $%.2f\n", gap, money);
                total += money;
            }

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

    return 0;
}

发表评论