1016 Phone Bills (25 分)

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.

Input Specification:

Each input file contains one test case. Each case has two parts: the rate structure, and the phone call records.

The rate structure consists of a line with 24 non-negative integers denoting the toll (cents/minute) from 00:00 - 01:00, the toll from 01:00 - 02:00, and so on for each hour in the day.

The next line contains a positive number N (≤1000), followed by N lines of records. Each phone call record consists of the name of the customer (string of up to 20 characters without space), the time and date (mm:dd:hh:mm), and the word on-line or off-line.

For each test case, all dates will be within a single month. Each on-line record is paired with the chronologically next record for the same customer provided it is an off-line record. Any on-line records that are not paired with an off-line record are ignored, as are off-line records not paired with an on-line record. It is guaranteed that at least one call is well paired in the input. You may assume that no two records for the same customer have the same time. Times are recorded using a 24-hour clock.

Output Specification:

For each test case, you must print a phone bill for each customer.

Bills must be printed in alphabetical order of customers' names. For each customer, first print in a line the name of the customer and the month of the bill in the format shown by the sample. Then for each time period of a call, print in one line the beginning and ending time and date (dd:hh:mm), the lasting time (in minute) and the charge of the call. The calls must be listed in chronological order. Finally, print the total charge for the month in the format shown by the sample.

Sample Input:

10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
10
CYLL 01:01:06:01 on-line
CYLL 01:28:16:05 off-line
CYJJ 01:01:07:00 off-line
CYLL 01:01:08:03 off-line
CYJJ 01:01:05:59 on-line
aaa 01:01:01:03 on-line
aaa 01:02:00:01 on-line
CYLL 01:28:15:41 on-line
aaa 01:05:02:24 on-line
aaa 01:04:23:59 off-line

Sample Output:

CYJJ 01
01:05:59 01:07:00 61 $12.10
Total amount: $12.10
CYLL 01
01:06:01 01:08:03 122 $24.40
28:15:41 28:16:05 24 $3.85
Total amount: $28.25
aaa 01
02:00:01 04:23:59 4318 $638.80
Total amount: $638.80

解题思路

  1. 使用map来保存每个用户的账单,因为一个用户存在多条记录,所以使用map >来保存输入的数据
  2. 遍历每个用户的账单,将vector进行排序(按时间从大到小),逆序遍历vector,将最后一个元素pop,如果这个元素是off-line,则继续pop,否则,查看vector中下一个元素,如果这个元素是off-line,则配对成功,将其pop,否则,使用这个元素与下一个元素进行比较。
  3. 如何计算money?
    on-line和off-line相隔的天数 + off-line的时分所花费的钱 - on-line时分的钱。举例:01:06:01 02:08:03,则他所花费的钱是

代码

#include
#include
#include
#include
#include
using namespace std;

int rate[24];
struct record {
    string time;
    bool online;
};

map > bill;

bool cmp(record r1, record r2) {
    return r1.time > r2.time;
}

int time2sec(string time) {
    time = time.substr(3);
    int day = (time[0] - '0')*10 + (time[1] - '0');
    int hour = (time[3] - '0')*10 + (time[4] - '0'); 
    int min = (time[6] - '0')*10 + (time[7] - '0');
    int all_min = day*24*60 + hour*60 + min;
    return all_min;
}

double money(string time1, string time2) {
    time1 = time1.substr(3);
    time2 = time2.substr(3);
    int delta_day = ((time2[0] - '0')*10 + (time2[1] - '0')) - 
                        ((time1[0] - '0')*10 + (time1[1] - '0'));
    double all_money = 0;
    int day_rate = 0;
    for (int i = 0; i < 24; ++i)
    {
        day_rate += rate[i]*60;
    }
    all_money += day_rate*delta_day;

    int starthour = (time1[3] - '0')*10 + (time1[4] - '0');
    int startmin = (time1[6] - '0')*10 + (time1[7] - '0');

    int endhour = (time2[3] - '0')*10 + (time2[4] - '0'); 
    int endmin = (time2[6] - '0')*10 + (time2[7] - '0');

    int startmoney = 0;
    int i = 0;
    for (; i < starthour; ++i)
    {
        startmoney += rate[i]*60;
    }
    startmoney += rate[i]*startmin;

    int endmoney = 0;
    i = 0;
    for (; i < endhour; ++i)
    {
        endmoney += rate[i]*60;
    }
    endmoney += rate[i]*endmin;

    all_money += endmoney - startmoney;
    return all_money * 0.01;
}

string format_money(double money) {
    string money_str = to_string(money);
    return money_str;
}

int main(){
    for (int i = 0; i < 24; ++i)
    {
        cin >> rate[i];
    }

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        string name, time, flag;
        cin >> name >> time >> flag;
        if(bill.find(name) == bill.end()) {
            vector v;
            bill[name] = v;
        }

        record r;
        r.time = time;
        r.online = flag == "on-line"? true: false;
        bill[name].push_back(r);
    }

    for (map >::iterator it = bill.begin(); it != bill.end(); ++it)
    {
        vector records = it->second;
        sort(records.begin(), records.end(), cmp);
        int cnt = 0;
        string month_str = records[0].time.substr(0, 2);
        double amount = 0;
        while(records.size() >= 2) {
            record temp = records[records.size()-1];
            records.pop_back();
            if(!temp.online) continue;
            else {
                if(!records[records.size()-1].online) {
                    cnt++;
                    if(cnt == 1) {
                        cout << it->first << " " << month_str << endl;
                    }
                    double m = 0;
                    m = money(temp.time, records[records.size()-1].time);
                    amount += m;
                    cout << temp.time.substr(3) << " " << records[records.size()-1].time.substr(3) << " " << 
                                time2sec(records[records.size()-1].time)-time2sec(temp.time) << " $";
                    printf("%.2lf\n", m);
                    records.pop_back();
                }
            }
        }
        if(cnt > 0) {
            printf("Total amount: $%.2lf\n", amount);
        }
        
    }
}

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