1016. Phone Bills (25)

来源:互联网 发布:杭电网络攻防平台 编辑:程序博客网 时间:2024/05/20 01:45

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.80Total amount: $638.80

#include<iostream>#include<map>#include<string>#include<algorithm>#include<vector>using namespace std;map<string,int> ma;int cost[24];int all_day=0;struct record{string name;int state;int time;record* Next;};vector<string> ve;struct time_record;struct person_record{time_record* Next;time_record* now;int sum;};struct time_record{int time_begin;int time_end;int state;time_record* Next;};bool compare1(record &a,record &b){return a.time<b.time;}void insert1(int time,int state,int i,person_record *person){if(person[i].Next==NULL){if(state==2)return;person[i].Next=new time_record;person[i].now=person[i].Next;person[i].now->state=1;person[i].now->Next=NULL;person[i].now->time_begin=time;person[i].now->time_end=-1;return;}if(state==1){if(person[i].now->state==1){person[i].now->time_begin=time;return;}person[i].now->Next=new time_record;person[i].now=person[i].now->Next;person[i].now->state=1;person[i].now->Next=NULL;person[i].now->time_begin=time;person[i].now->time_end=-1;return;}else{if(person[i].now->state==1){person[i].now->time_end=time;person[i].now->state=2;return;}return;}}void disp_time(int t){printf("%02d:%02d:%02d",t/(24*60),t%(24*60)/60,t%60);}double caclulate_money(int begin,int end){double sum=-all_day*0.6;int i;for(i=begin/(24*60);i<end/(24*60);i++)sum+=all_day*0.6;for(i=0;i<=end%(24*60)/60;i++)sum+=cost[i]*0.6;sum-=cost[i-1]*0.01*(60-end%60);for(i=23;i>=begin%(24*60)/60;i--)sum+=cost[i]*0.6;sum-=cost[i+1]*0.01*(begin%60);printf("%.2f",sum);return sum;}int main(){int N;int i;int count=0;int mo,d,h,m;for(i=0;i<24;i++){cin>>cost[i];all_day+=cost[i];}cin>>N;string temp;string s;record *r=new record[N];person_record *person=new person_record[N];for(i=0;i<N;i++){person[i].Next=NULL;person[i].now=NULL;cin>>r[i].name;scanf("%d:%d:%d:%d",&mo,&d,&h,&m);r[i].time=d*24*60+h*60+m;cin>>s;if(s[1]=='n')r[i].state=1;elser[i].state=2;}sort(r,r+N,compare1);for(i=0;i<N;i++){//cout<<r[i].name<<" "<<r[i].time<<" "<<r[i].state<<endl;}map<string ,int>::iterator it;for(i=0;i<N;i++){it=ma.find(r[i].name);if(it!=ma.end()){insert1(r[i].time,r[i].state,it->second,person);//cout<<"insert:"<<it->second<<" "<<it->first<<" ";disp_time(r[i].time);cout<<endl;}else{ma.insert(pair<string,int>(r[i].name,count++));insert1(r[i].time,r[i].state,count-1,person);//cout<<"insert:"<<count-1<<" "<<r[i].name<<" ";disp_time(r[i].time);cout<<endl;}}time_record *tt;for(it=ma.begin();it!=ma.end();it++){ve.push_back(it->first);}sort(ve.begin(),ve.end());for(i=0;i<ve.size();i++){it=ma.find(ve[i]);tt=person[it->second].Next;if(tt!=NULL){if(tt->state!=1){double sum=0;cout<<it->first;printf(" %02d\n",mo);while(tt!=NULL){if(tt->state==2){disp_time(tt->time_begin);cout<<" ";disp_time(tt->time_end);printf(" %d $",tt->time_end-tt->time_begin);sum+=caclulate_money(tt->time_begin,tt->time_end);cout<<endl;}tt=tt->Next;} printf("Total amount: $%.2f\n",sum); }} }} 

感想:

这道题烦的一比,做了我两个小时,要是没有STL估计得弄个一辈子才能弄出来。。。。

数据结构:先用map映射,把每个出现的客户都映射到一个链表头部,这个链表头部还有一个指向操作单元的指针,链表后每个单元是每次通话的时间

流程:

1.先对记录按时间排序

2.建立如上数据结构

3.根据题目要求依次插入记录

4.把map的数据转存到vector中并安string类排序规则排序

5.遍历vector和与之相连的链表

6.如果有没有话费的用户就不输出

0 0
原创粉丝点击