【PAT】1016. Phone Bills (25)

来源:互联网 发布:sql语句别名 编辑:程序博客网 时间:2024/04/27 20:41

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 1010CYLL 01:01:06:01 on-lineCYLL 01:28:16:05 off-lineCYJJ 01:01:07:00 off-lineCYLL 01:01:08:03 off-lineCYJJ 01:01:05:59 on-lineaaa 01:01:01:03 on-lineaaa 01:02:00:01 on-lineCYLL 01:28:15:41 on-lineaaa 01:05:02:24 on-lineaaa 01:04:23:59 off-line
Sample Output:
CYJJ 0101:05:59 01:07:00 61 $12.10Total amount: $12.10CYLL 0101:06:01 01:08:03 122 $24.4028:15:41 28:16:05 24 $3.85Total amount: $28.25aaa 0102:00:01 04:23:59 4318 $638.80

Total amount: $638.80

分析:首先进行on-line 和 off-line的匹配,而后按照时间先后输出。这道题目感觉有坑,当我用以下代码的cmp2进行按时间排序后反而不能通过。不明白为什么。后来把cmp2排序注释掉反而能够通过了。以下是可以AC的代码。

#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;int rate[24];int dayCost;struct node{node(string nam, int mon,int d, int h, int m, string sta){name = nam;month = mon;day = d;hour = h;min = m;status = sta;flag = false;}bool flag;double cost;int time;string name;int month,day,hour,min;string status;};struct result{string name;int month;int day;int hour;int min;vector<node> vec;double totalCost;};bool cmp(node a, node b){if(a.name != b.name){return a.name < b.name;}if(a.month != b.month){return a.month<b.month;}if(a.day != b.day){return a.day < b.day;}if(a.hour != b.hour){return a.hour < b.hour;}if(a.min!=b.min){return a.min < b.min;}}bool cmp2(result a, result b){ if(a.month != b.month){return a.month<b.month;}if(a.day != b.day){return a.day < b.day;}if(a.hour != b.hour){return a.hour < b.hour;}if(a.min!=b.min){return a.min < b.min;}}void countCost(node &last, node& now){int dFrom = last.day;int hFrom = last.hour;int mFrom = last.min;int dTo = now.day;int hTo = now.hour;int mTo = now.min;double cost = 0;int time = 0;int i,j;if(dFrom == dTo){//同一天if(hFrom == hTo){//同一小时cost += rate[hFrom]*(mTo-mFrom); time += mTo-mFrom;}else{//跨小时了cost += rate[hFrom]*(60-mFrom);time += 60-mFrom;for(i=hFrom+1; i<hTo; i++){cost += rate[i]*60;time += 60;} cost += rate[hTo]*mTo;time += mTo;}}else{//跨天了  cost += rate[hFrom]*(60-mFrom);//第一个小时费用time += 60-mFrom;//第一天费用for(i=hFrom+1; i<=23; i++){cost += rate[i]*60;time += 60;}//中间天费用 for(i=dFrom+1; i<dTo; i++){cost += dayCost*60;time += 1440;}//最后一天费用 for(i=0; i<hTo; i++){cost += rate[i]*60;time += 60;}//最后一小时费用 cost += rate[hTo]*mTo;time += mTo;}last.cost = 0;now.cost = cost;now.time = time;}bool cmpTime(result &t, node &n){if(t.month > n.month){return true;}if(t.month == n.month){if(t.day > n.day){return true;}else if(t.day == n.day){if(t.hour > n.hour){return true;}else if(t.hour == n.hour){if(t.min > n.min){return true;}}}}return false;}int main(int argc, char** argv) {int i, n, t, index;dayCost = 0;for(i=0; i<24; i++){cin>>rate[i];dayCost += rate[i];}cin>>n;string name, status;int month,day,hour,min;char ch;vector<node> vec; for(i=0; i<n; i++){cin>>name;cin>>month>>ch>>day>>ch>>hour>>ch>>min;cin>>status;vec.push_back(node(name,month,day,hour,min,status));}sort(vec.begin(), vec.end(), cmp);vector<result> out; for(vector<node>::iterator it=vec.begin()+1; it!=vec.end(); it++){node now = *it;node last = *(it-1);if(now.name == last.name){//是同一个人if(last.status=="on-line"  ){//配对成功if(now.status == "off-line"){result t;countCost(last, now);if(out.size()!=0 && out.back().name == now.name){//这个人存在多条记录 out.back().totalCost += now.cost;out.back().vec.push_back(last);out.back().vec.push_back(now);if( cmpTime(out.back(), last) ){out.back().month = last.month;out.back().day = last.day;out.back().hour = last.hour;out.back().min = last.min;}}else{ t.totalCost = 0;t.name = now.name; t.totalCost += now.cost;t.vec.push_back(last);t.vec.push_back(now);t.month = last.month;t.day = last.day;t.hour = last.hour;t.min = last.min;out.push_back(t);  }  }  } }  } //sort(out.begin(), out.end(), cmp2);//for(i=0; i<out.size(); i++){//cout<<out[i].name<<" "<<out[i].month<<":"<<out[i].day<<":"<<out[i].hour<<":"<<out[i].min<<endl;//}int j;for(i=0; i<out.size(); i++){result tmp = out[i];cout<<tmp.name;printf(" %02d\n",tmp.month);vector<node> v(tmp.vec);for(j=0; j<v.size(); j++){if(v[j].status == "on-line"){printf("%02d:%02d:%02d",v[j].day, v[j].hour, v[j].min);}else{printf(" %02d:%02d:%02d %d $%.2f\n",v[j].day, v[j].hour, v[j].min,v[j].time, (double)v[j].cost/100);} }printf("Total amount: $%.2f\n",tmp.totalCost/100);}return 0;}


0 0