【PAT】A1016. Phone Bills(sort)

[PAT]A1016. Phone Bills(sort)

@(PAT)

Links: https://www.patest.cn/contests/pat-a-practise/1016

Title:
Enter the rate of telephone charges per hour, and then calculate and print each person's telephone charges according to the records given. Records consist of different times and corresponding up-line and down-line. Only when the adjacent up-line and down-line match, can they be regarded as effective records.
Attention should be paid to:
1. The rate of telephone calls per hour is different and should be handled specially.
2. Only close matches between the up-line and down-line are valid records.
3. The output of the results is sorted in descending order according to the dictionary order of each person's ID, and the effective records of the output are sorted in ascending order according to time.

Train of thought:
1. Use struct to store each record, put it into vector, and sort it.
2. Match valid records and put them into a vector.
3. Processing each valid record, calculating their cost and time, focusing on the compilation of the cost function. See the code below.

My AC code:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

vector<int> rate;

struct Record
{
    string name;
    int month;
    int day;
    int hour;
    int minute;
    // true for on, false for off
    bool on_off_line;
    Record(string n, int mo, int d, int h, int mi, bool on_off) {
        name = n;
        month = mo;
        day = d;
        hour = h;
        minute = mi;
        on_off_line = on_off;
    }
    void printTime() {
        printf("%02d:%02d:%02d ", this->day, this->hour, this->minute);
    }
};

bool cmp(Record r1, Record r2) {
    if (r1.name != r2.name) return r1.name < r2.name;
    if (r1.month != r2.month) return r1.month < r2.month;
    if (r1.day != r2.day) return r1.day < r2.day;
    if (r1.hour != r2.hour) return r1.hour < r2.hour;
    if (r1.minute != r2.minute) return r1.minute < r2.minute;
}

float calculateCharge(Record r1, Record r2) {
    int min1 = r1.day * 24 * 60 + r1.hour * 60 + r1.minute;
    int min2 = r2.day * 24 * 60 + r2.hour * 60 + r2.minute;
    printf("%d ", min2 - min1);
    int day = r1.day;
    int charge= 0;
    if (r2.day == r1.day) {
        if (r2.hour == r1.hour) {
            charge = (r2.minute - r1.minute)* rate[r1.hour];
        }
        else {
            for (int i = r1.hour; i <= r2.hour; i++) {
                if (i == r1.hour) {
                    charge += (60 - r1.minute)* rate[i];
                }
                else if (i == r2.hour) {
                    charge += r2.minute* rate[i];
                }
                else {
                    charge += 60 * rate[i];
                }
            }
        }
    }
    else {
        for (int i = r1.day; i <= r2.day; i++) {
            if (i == r1.day) {
                for (int j = r1.hour; j <= 23; j++) {
                    if (j == r1.hour) {
                        charge += (60 - r1.minute)* rate[j];
                    }
                    else {
                        charge += 60 * rate[j];
                    }
                }
            }
            else if (i== r2.day) {
                for (int j = 0; j <= r2.hour; j++) {
                    if (j == r2.hour) {
                        charge += r2.minute* rate[j];
                    }
                    else {
                        charge += 60 * rate[j];
                    }
                }
            }
            else {
                for (int j = 0; j < 24; j++) {
                    charge += 60 * rate[j];
                }
            }
        }
    }
    float c = (float)charge / 100;
    printf("$%.2f\n", c);
    return c;
}

int main() {
    // vector<int> rate;
    for (int i = 0; i < 24; i++) {
        int rtemp;
        scanf("%d", &rtemp);
        rate.push_back(rtemp);
    }
    int n;
    vector<Record> records;
    scanf("%d", &n);
    while (n--) {
        string name, time, line;
        cin >> name >> time >> line;
        int month = (time[0] - '0') * 10 + time[1] - '0';
        int day= (time[3] - '0') * 10 + time[4] - '0';
        int hour= (time[6] - '0') * 10 + time[7] - '0';
        int min= (time[9] - '0') * 10 + time[10] - '0';
        bool on_or_off;
        if (line == "on-line") {
            on_or_off = 1;
        }
        else {
            on_or_off = 0;
        }
        Record re(name, month, day, hour, min, on_or_off);
        records.push_back(re);
    }
    sort(records.begin(), records.end(), cmp);
    vector<Record> rec_used;
    for (int i = 1; i < records.size(); i++) {
        if (records[i].name== records[i- 1].name&& records[i].on_off_line == 0 && records[i - 1].on_off_line == 1) {
            rec_used.push_back(records[i- 1]);
            rec_used.push_back(records[i]);
        }
    }

    vector<int> index;
    index.push_back(0);
    for (int i = 1; i < rec_used.size(); i++) {
        if (rec_used[i].name != rec_used[i - 1].name) {
            index.push_back(i);
        }
    }
    index.push_back(rec_used.size());
    int user_num = index.size()- 1;

    for (int i = 0; i < user_num; i++) {
        cout << rec_used[index[i]].name << " ";
        printf("%02d\n", rec_used[index[i]].month);
        int begin = index[i];
        int end = index[i + 1]- 1;
        float total_charge = 0.0;
        for (int j = begin; j <= end; j++) {
            rec_used[j].printTime();
            if (j % 2 == 1) {
                total_charge+= calculateCharge(rec_used[j - 1], rec_used[j]);
            }
        }
        printf("Total amount: $%.2f\n", total_charge);
    }

    return 0;
}

Posted by ade1982 on Tue, 05 Feb 2019 15:48:17 -0800