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<string, vector<record> >来保存输入的数据
  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,则他所花费的钱是(2-1)*\sum_{i=0}^{24} rate[i]*60 + \sum_{i=0}^{8}(rate[i]*60) + 3*rate[8] - \sum_{i=0}^{6}(rate[i]*60) - 1*rate[6]

代码

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<queue>
using namespace std;

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

map<string, vector<record> > 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<record> v;
            bill[name] = v;
        }

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

    for (map<string, vector<record> >::iterator it = bill.begin(); it != bill.end(); ++it)
    {
        vector<record> 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);
        }
        
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容