pat tricksPTA甲级

结构体排序➕分时计价 | 1016 Phone Bills (2

2019-01-27  本文已影响0人  zilla

1016 Phone Bills
想想就排个序,配个对,算算价格,然而好久才ac啊(

就差个等号 差2个case 6分呢 > >=
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>

using namespace std;
const int LON = strlen("on-line");
int rate[24], nn, month;
struct Record {
    char name[22];
    int mdhm[4];
    int state;//0,1
} records[1010];
struct Pay {
    int r_1;
    int r;
    int time;
    int price;
};
vector<Pay> personal_pay;

int cmp(const void *re1, const void *re2) {
    Record r1 = *(Record *) re1, r2 = *(Record *) re2;
    int res = strcmp(r1.name, r2.name);
    if (res != 0)return res;
    int a, b;
    for (int i = 1; i < 4; ++i) {
        a = r1.mdhm[i], b = r2.mdhm[i];
        if (a != b)
            return a - b;
    }
    res = r1.state - r2.state;
    if (res != 0)return res;
    return 0;
}

void print_payinfo(char name[]) {
    if (!personal_pay.empty()) {
        printf("%s %02d\n", name, month);
        int size = personal_pay.size(), total = 0;
        for (int i = 0; i < size; ++i) {
            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%d.%02d\n",
                   records[personal_pay[i].r_1].mdhm[1], records[personal_pay[i].r_1].mdhm[2],
                   records[personal_pay[i].r_1].mdhm[3], records[personal_pay[i].r].mdhm[1],
                   records[personal_pay[i].r].mdhm[2], records[personal_pay[i].r].mdhm[3],
                   personal_pay[i].time, personal_pay[i].price / 100, personal_pay[i].price % 100);
            total += personal_pay[i].price;
        }
        printf("Total amount: $%d.%02d\n", total / 100, total % 100);
    }
    personal_pay.clear();
}

int main() {
    int d_fare = 0;
    for (int i = 0; i < 24; i++) {
        scanf("%d", &rate[i]);
        d_fare += rate[i];
    }
    d_fare *= 60;
    scanf("%d", &nn);
    char str[10];
    for (int j = 0; j < nn; ++j) {
        scanf("%s%d:%d:%d:%d%s", records[j].name,
              &records[j].mdhm[0], &records[j].mdhm[1],
              &records[j].mdhm[2], &records[j].mdhm[3], str);
        if (strlen(str) == LON)
            records[j].state = 0;
        else records[j].state = 1;
    }
    month = records[0].mdhm[0];
    qsort(records, nn, sizeof(Record), cmp);
    char name[22] = "";
    for (int z = 0; z < nn; ++z) {
        if (strcmp(name, records[z].name) == 0) {
            if (records[z - 1].state == 0 && records[z].state == 1) {
                int md = 0, mh, mm, fare = 0;
                md = records[z].mdhm[1] - records[z - 1].mdhm[1];
                mh = (records[z].mdhm[2] - records[z - 1].mdhm[2]);
                mm = records[z].mdhm[3] - records[z - 1].mdhm[3];
                fare += md * d_fare;
                if (mh <= 0) {
                    fare -= d_fare;
                    for (int i = records[z - 1].mdhm[2] + 1; i < 24; ++i) {
                        fare += rate[i] * 60;
                    }
                    for (int i = 0; i < records[z].mdhm[2]; ++i) {
                        fare += rate[i] * 60;
                    }
                } else {
                    for (int i = records[z - 1].mdhm[2] + 1; i < records[z].mdhm[2]; ++i) {
                        fare += rate[i] * 60;
                    }
                }
                int time = md * 24 * 60 + mh * 60 + mm;
                fare += (60 - records[z - 1].mdhm[3]) * rate[records[z - 1].mdhm[2]];
                fare += records[z].mdhm[3] * rate[records[z].mdhm[2]];
                Pay pay = {z - 1, z, time, fare};
                personal_pay.push_back(pay);
            }
        } else {
            print_payinfo(name);
            strcpy(name, records[z].name);
        }
    }
    print_payinfo(name);
    return 0;
}

想像一个“时钟”/“进制(各位权重不同,其中小时这一位,每个”单位”权重不同)” ,可以写出较为简洁的 计价函数。
今天状态是真的不好。。。
也可以像晴神笔记上那样,一直++进位什么的,好想一些~~

#include <cstdio>
#include <string>
#include <algorithm>

using namespace std;
int charge24[24], nn, dcharge = 0;

struct Rec {
    string name;
    int dd, hh, mm;
    int type; //on0 off1
    bool operator<(const Rec &r2) const {
        if (name != r2.name)
            return name < r2.name;
        if (dd != r2.dd)
            return dd < r2.dd;
        if (hh != r2.hh)
            return hh < r2.hh;
        return mm < r2.mm;
    }
} recs[1010];

int getTime(int on, int off) {
    return (recs[off].dd - recs[on].dd) * 24 * 60 +
           (recs[off].hh - recs[on].hh) * 60 +
           recs[off].mm - recs[on].mm;
}

int getFare(int on, int off) {
    int d1, d2, h1, h2, m1, m2, res = 0;
    d1 = recs[on].dd, d2 = recs[off].dd;
    h1 = recs[on].hh, h2 = recs[off].hh;
    m1 = recs[on].mm, m2 = recs[off].mm;
    res += (d2 - d1) * dcharge;
    int st, ed, rate1;
    if (h2 - h1 < 0) {
        st = h2, ed = h1, rate1 = -1;
    } else {
        st = h1, ed = h2, rate1 = 1;
    }
    int temp = 0;
    while (st < ed) {
        temp += 60 * charge24[st++];
    }
    res += temp * rate1;
    res += (charge24[h2] * m2 - charge24[h1] * m1);
    return res;
}

int main() {
    for (int i = 0; i < 24; ++i) {
        scanf("%d", &charge24[i]);
        dcharge += 60 * charge24[i];
    }
    scanf("%d", &nn);
    char name[22], type[12];
    int month, tp;
    for (int i = 0; i < nn; ++i) {
        scanf("%s%d:%d:%d:%d%s", name, &month, &recs[i].dd, &recs[i].hh, &recs[i].mm, type);
        if (type[1] == 'n')
            tp = 0;
        else tp = 1;
        recs[i].name = name, recs[i].type = tp;
    }
    sort(recs, recs + nn);
    string curr_user;
    int on, off, total = 0;
    bool printed = false;
    for (int i = 0; i <= nn; ++i) {
        if (curr_user != recs[i].name) {

            if (total > 0) {
                printf("Total amount: $%d.%02d\n", total / 100, total % 100);
                if (i == nn) break;
            }
            curr_user = recs[i].name;
            printed = false;
            total = 0;
        }
        if (recs[i].name == curr_user && recs[i].type == 0 &&
            recs[i + 1].name == curr_user && recs[i + 1].type == 1) {
            if (printed == false) {
                printf("%s %02d\n", curr_user.data(), month);
                printed = true;
            }
            on = i, off = i + 1;
            int price = getFare(on, off);
            total += price;
            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%d.%02d\n",
                   recs[on].dd, recs[on].hh, recs[on].mm,
                   recs[off].dd, recs[off].hh, recs[off].mm,
                   getTime(on, off), price / 100, price % 100);
            i++;
        }
    }
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读