您的位置:首页 > 其它

1016. Phone Bills (25)

2017-04-18 17:16 344 查看
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


借鉴了一下别人的思路,大概是按名称再按时间排序,再将匹配的信息插入vector 同一个人遇到online则判断前一个是不是offline,如果是online则弹出前一个后再插入是offline则直接插入, 遇到offline则判断前一个是不是online,是online直接插入,不是则不操作;

测试点1有一条是最后一个人为空,有一条是其中有个人为空

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iterator>
#include <algorithm>
#include <iomanip>
using namespace std;

struct info{
string name;
string time;
string status;
};
const string on = "on-line";
const string off = "off-line";

bool cmp(const info &a, const info &b)
{
if(a.name < b.name)
return true;
else if(a.name==b.name && a.time<b.time)
4000

return true;
return false;
}
vector<info> record;
vector<info> person;
int toll[24];

void solve(vector<info> p);
double amount(string on, string off);
int main()
{
int n;
info temp;
for(int i=0; i<24; i++)
cin >> toll[i];
cin >> n;
cin.get();
for(int i=0; i<n; i++)
{
cin >> temp.name >> temp.time >> temp.status;
record.push_back(temp);
}
sort(record.begin(), record.end(), cmp);//按名字顺序如果名字相同则按时间顺序排
for(int i=0; i<n; i++)
{
if(i==0 || (i!=0 && record[i].name==record[i-1].name))//当前面的数据为同一个人时
{
if(person.empty())
{
if(record[i].status == on)
person.push_back(record[i]);
}
else{
if(record[i].status == on)//如果即将插入数据为online则看尾部数据是online就弹出尾部数据,插入该数据
{
if(person.back().status == on)
{
person.pop_back();
person.push_back(record[i]);
}
else{//为offline则匹配直接插入
person.push_back(record[i]);
}
}
else{//插入数据为offline则判断尾部是online则插入不是什么都不做
if(person.back().status == on)
{
person.push_back(record[i]);
}
}
}
}
else {//当名字更换时 即前一个名字和当前名字不同时
if(!person.empty())//如果前面统计非空需要判断尾部是否是online
{
if(person.back().status == on)
person.pop_back();
}
if(!person.empty())//弹出尾部后依旧非空则计算详细
solve(person);
person.clear();
if(record[i].status == on)//该数据为online可插入
person.push_back(record[i]);
}
}
if(!person.empty())//最后一个人因为不会进去上面的else所以单独处理
{
if(person.back().status == on)
person.pop_back();
if(!person.empty())
solve(person);
}
return 0;
}

void solve(vector<info> p)
{
double totalamount = 0;
string online;
string offline;
vector<info>::size_type i;
cout << p[0].name << " " << p[0].time[0] << p[0].time[1] << endl;
for(i=0; i<p.size(); i+=2)
{
online = p[i].time.substr(3);//取天:时:分
offline = p[i+1].time.substr(3);
totalamount += amount(online, offline);
}
cout << fixed << setprecision(2) << "Total amount: $" << totalamount << endl;
}

double amount(string on, string off)
{
double total = 0;
int ondd, onhh, onmm, offdd, offhh, offmm;
int minute = 0;
ondd = (on[0]-'0')*10+(on[1]-'0');//分离出开始和结束的天时分并计算
onhh = (on[3]-'0')*10+(on[4]-'0');
onmm = (on[6]-'0')*10+(on[7]-'0');
offdd = (off[0]-'0')*10+(off[1]-'0');
offhh = (off[3]-'0')*10+(off[4]-'0');
offmm = (off[6]-'0')*10+(off[7]-'0');
while(ondd!=offdd || onhh!=offhh || onmm!=offmm)
{
total += toll[onhh];
minute++;
onmm++;
if(onmm >= 60)
{
onmm = 0;
onhh++;
if(onhh >= 24)
{
onhh = 0;
ondd++;
}
}
}
cout << fixed << setprecision(2) << on << " " << off << " " << minute << " $" << total/100 << endl;
return total/100;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: