码迷,mamicode.com
首页 > 其他好文 > 详细

1016 Phone Bills

时间:2019-11-24 00:16:29      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:ber   pre   air   prepare   def   set   用户   nes   type   

先介绍本章(我新用上了的)新东西、最佳神器:priority_queue。妈妈再也不用担心我花时间写队列排序啦!

需求:#include<queue>

用法:priority_queue<T, vector<T>, greater<T>>(升序)/priority_queue<T, vector<T>, less<T>>(降序)

vector其实不需要写,但是因为关键的升降序的说明是第三个参数故而第二个参数必须带一下。

因为其实和queue所需的函数一样(升降序本质也就只是大小于的应用),如果是queue<T>可以直接用的T那可以直接用priority_queue。

priority_queue中的函数会自动排好。例如greater的话,top()和pop()就是队列中最小的那一个。

 

做题逻辑……反正按人往他队列里塞时间点(把4个时间数据和online offline状况全都用一个int表示了方便排序),然后对人名排一下序。

最后输出时,tempii=top,pop,tempiii=top,判定tempii是online,iii是offline就开始处理输出,不然直接continue(已经pop掉了不要的数据了)。

 

然后本题测试样例坑点:有两个测试点里有“全账单完全无效的用户”,对于他们,应不输出任何东西。我一开始是先按用户输出第一行信息然后再做他的账单的判定、输出的于是就错了(摊手)。

 

 

 

 

 

原题:

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 (≤), 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-linerecord. 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

代码:
#include<iostream>
#include<string>
#include<queue>

using namespace std;


typedef struct
{
    string name;
    priority_queue<int, vector<int>, greater<int>>* bills = NULL;
}person;

int main()
{
    int hour[24];
    int i;
    for (i = 0; i < 24; i++)
        cin >> hour[i];
    int n;
    cin >> n;
    person* people;
    people = new person[n + 1];
    int ii;
    string name;
    int tempi;
    int timeonline;
    char c;
    string temps;
    for (i = 0; i < n; i++)
    {
        cin >> name;
        ii = 0;
        while (people[ii].bills != NULL)
        {
            if (people[ii].name == name)
                break;
            ii++;
        }
        if (people[ii].bills == NULL)
        {
            people[ii].name = name;
            people[ii].bills = new priority_queue<int, vector<int>, greater<int>>;
        }
        timeonline = 0;
        cin >> tempi;
        timeonline += tempi;
        cin >> c >> tempi;
        timeonline *= 100;
        timeonline += tempi;
        cin >> c >> tempi;
        timeonline *= 100;
        timeonline += tempi;
        cin >> c >> tempi;
        timeonline *= 100;
        timeonline += tempi;
        cin >> temps;
        timeonline *= 10;
        if (temps == "on-line")
            timeonline += 1;
        else if(temps == "off-line")
            timeonline += 2;
        //timeonline=mmddhhmmo(o代表online offline情况)
        people[ii].bills->push(timeonline);
    }
    //至此写入结束

  //人名排序 priority_queue
<int, vector<int>, greater<int>>* tempq; for (i = 0; people[i].bills!=NULL ; i++) { for (ii = i + 1; people[ii].bills != NULL; ii++) { if (people[i].name > people[ii].name) { tempq = people[ii].bills; people[ii].bills = people[i].bills; people[i].bills = tempq; temps = people[ii].name; people[ii].name = people[i].name; people[i].name = temps; } } }
  //时间数据处理&&输出
int tempii; int total; int abill; int time; int tempiii; bool hadbill; for (i = 0; people[i].bills!=NULL; i++) { hadbill = false; total = 0; time = 0; while (!people[i].bills->empty()) { abill = 0; tempi = people[i].bills->top(); people[i].bills->pop(); if (!people[i].bills->empty()) tempii = people[i].bills->top(); else break; if (tempi % 10 != 1 || tempii % 10 != 2)//个位是关于online和offline的指标 continue; else if (!hadbill)//确认了这个人有有效数据后再输出他的第一行信息 { cout << people[i].name << " "; if (tempi/ 10000000 < 10) cout << "0"; cout << tempi / 10000000 << endl; hadbill = true; }        //以下大括号纯粹是删掉了一个if后又不想修改空格数() { time = 0; tempi /= 10; tempii /= 10; tempi %= 1000000; tempii %= 1000000; ii = tempi; while (ii < tempii) { time++; abill += hour[(ii % 10000 - ii % 100) / 100]; ii++; if (ii % 100 == 60) { ii += 40; if (ii % 10000 == 2400) { ii -= 2400; ii += 10000; } } } tempiii = tempi / 10000; if (tempiii < 10)cout << "0"; cout << tempiii << ":"; tempiii = (tempi / 100) % 100; if (tempiii < 10)cout << "0"; cout << tempiii << ":"; tempiii = tempi % 100; if (tempiii < 10)cout << "0"; cout << tempiii; cout << " "; tempiii = tempii / 10000; if (tempiii < 10)cout << "0"; cout << tempiii << ":"; tempiii = (tempii / 100) % 100; if (tempiii < 10)cout << "0"; cout << tempiii << ":"; tempiii = tempii % 100; if (tempiii < 10)cout << "0"; cout << tempiii; cout << " "<< time << " $"; if (abill < 100) cout << "0."; else cout << abill / 100 << "."; if (abill % 100 < 10) cout << "0"; cout << abill % 100 << endl; total += abill; } } if (!hadbill) continue; cout << "Total amount: $"; if (total < 100) cout << "0."; else cout << total / 100 << "."; if (total % 100 < 10) cout << "0"; cout << total % 100 << endl; } return 0; }

 

1016 Phone Bills

标签:ber   pre   air   prepare   def   set   用户   nes   type   

原文地址:https://www.cnblogs.com/xujingspace/p/11920621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!