1016 Phone Bills (25 point(s))

1016 Phone Bills (25 point(s))

A long-distance telephone company charges its customers by the following rules:

Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records.

Example:

#include<iostream>
#include<cstdio>
#include<map>
#include<vector>

using namespace std;

int Stamp(string t) 
{ 
    int MM, dd, hh, mm;
    sscanf(t.c_str(), "%d:%d:%d:%d", &MM, &dd, &hh, &mm);
    return dd*1440+hh*60+mm; 
}

double Amount(int stamp, vector<int> &toll)
{
    double ans = stamp / 1440 * toll[24];
    int HH = 0;
    stamp %= 1440;
    while(stamp >= 60) {
        ans += 60 * toll[HH++%24];
        stamp -= 60;
    }
    ans += stamp * toll[HH%24];
    return ans/100;
}

void Bill(pair<string, map<string, string>> customer, vector<int> &toll)
{
    double total = 0;
    for(auto on = customer.second.begin(); on != customer.second.end(); on++)
        if(on->second == "off-line" && on != customer.second.begin()) {
            auto off = on--;
            if(on->second == "on-line") {
                int onStamp  = Stamp(on->first);
                int offStamp = Stamp(off->first);
                if(total == 0) cout << customer.first << ' ' << on->first.substr(0,2) << endl;
                cout << on->first.substr(3, 8) << ' ' << off->first.substr(3, 8) << ' ';
                cout << offStamp - onStamp << ' ';
                double sum = Amount(offStamp, toll) - Amount(onStamp, toll);
                printf("$%.2f\n", sum);
                total += sum;
            }
            ++on;
        }
    if(total) printf("Total amount: $%.2f\n", total);
}

int main()
{
    map<string, map<string, string>> record;
    vector<int> toll(25, 0);
    for(int i = 0; i < 24; i++) cin >> toll[i];
    for(int i = 0; i < 24; i++) toll[24] += toll[i];
    toll[24] *= 60;
    int N;
    cin >> N;
    for(int i = 0; i < N; i++) {
        string name, time, status;
        cin >> name >> time >> status;
        record[name][time] = status;
    }
    for(auto x : record) Bill(x, toll);
}

思路:

利用stl/map的红黑树实现原理,有序且查找快;

以当月0:0为基点,归一化处理时间戳 TimeStamp,然后计算/相减;

注意 Total Amount 为 0时,不用输出该用户信息。

猜你喜欢

转载自blog.csdn.net/u012571715/article/details/113986977