PTA 1016 Phone Bills

https://pintia.cn/problem-sets/994805342720868352/problems/994805493648703488
Map, vector, iterator.
Learned how to used STL.

#include 
#include 
#include 
#include 
#include 

using namespace std;
const int MAXN = 1001;

struct Record {
    string name;
    int sta, time, mon, dd, hh, min;//0表示on 1是off 
};

vector  reco(MAXN);
int cent[25];

int cmp(Record a, Record b) {
    return a.name != b.name ? a.name < b.name : a.time < b.time;
}

double calv(Record a, Record b) {
    int min = a.min, dd = a.dd, hh = a.hh;
    double v = 0;
    while (min < b.min || dd < b.dd || hh < b.hh) {
        min++;
        v += cent[hh];
        if (min == 60) {
            min = 0;
            hh++;
        }
        if (hh == 24) {
            dd++;
            hh = 0;
        }
    }
    return v/100.0;
}
int N;

int main() {

    for(int i = 0; i < 24; i++)
        scanf("%d", ¢[i]);

    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
        cin >> reco[i].name;
        char te[20];
        scanf("%d:%d:%d:%d %s", &reco[i].mon, &reco[i].dd, &reco[i].hh, &reco[i].min, te);
        reco[i].time = (reco[i].dd * 24 + reco[i].hh) * 60 + reco[i].min;
        reco[i].sta = (te[1] == 'n' ? 0 : 1);
    }

    sort(reco.begin(), reco.begin() + N, cmp);

    map > mp;
    for (int i = 1; i < N; i++) {
        if (reco[i].name == reco[i - 1].name && reco[i].sta && reco[i - 1].sta == 0) {
            mp[reco[i-1].name].push_back(reco[i - 1]);
            mp[reco[i-1].name].push_back(reco[i]);
        }
    }

    map >::iterator it;
    for (it = mp.begin(); it != mp.end(); it++) {
        vector vec = it->second;
        if (vec.size() == 0)
            continue;
        printf("%s %02d\n", vec[0].name.c_str(), vec[0].mon);
        double tv = 0;
        for (int i = 0; i < vec.size() - 1; i += 2) {
            Record r = vec[i];
            Record r2 = vec[i + 1];
            double v = calv(r, r2);
            tv += v;
            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", r.dd, r.hh, r.min,
            r2.dd, r2.hh, r2.min, r2.time - r.time, v);
        }
        printf("Total amount: $%.2f\n", tv);
    }

  return 0;
}

你可能感兴趣的:(PTA 1016 Phone Bills)