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

1016. Phone Bills (25)

时间:2015-02-27 00:15:02      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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 #include<stdio.h>
  2 #include<vector>
  3 #include<map>
  4 #include<algorithm>
  5 #include<string>
  6 #include<string.h>
  7 using namespace std;
  8 
  9 int rate[24];
 10 
 11 struct onecall
 12 {
 13     int mon,bdd,bhh,bmin,edd,ehh,emin,emon;
 14     int time;
 15     double money;
 16 };
 17 
 18 map<string,vector<onecall> > user ;
 19 
 20 
 21 
 22 struct record
 23 {
 24     char name[21];
 25     int mon,dd,hh,min;
 26     bool on;
 27 };
 28 
 29 
 30 
 31 map<string, vector<record> > mm;
 32 
 33 bool cmp (record a , record b)
 34 {
 35     if(a.mon != b.mon)
 36         return a.mon < b.mon;
 37 
 38     if(a.dd != b.dd)
 39         return a.dd < b.dd;
 40 
 41     if(a.hh != b.hh)
 42         return a.hh < b.hh;
 43 
 44     if(a.min != b.min)
 45         return a.min < b.min;
 46 }
 47 
 48 int main()
 49 {
 50     int i;
 51     int daycost = 0;
 52     for(i = 0 ;i < 24 ;i ++ )
 53     {
 54         scanf("%d",&rate[i]);
 55         daycost += rate[i] * 60;
 56     }
 57 
 58     int n;
 59     scanf("%d",&n);
 60     char name[21];
 61     int mon,dd,hh,min;
 62     char tem[21];
 63     vector<record> recordlist;
 64     recordlist.clear();
 65     for(i = 0 ; i < n ;i ++)
 66     {
 67         scanf("%s %d:%d:%d:%d %s",name,&mon,&dd,&hh,&min,tem);
 68         record rr;
 69         strcpy(rr.name,name);
 70         if(strcmp(tem,"on-line")== 0) rr.on = true;
 71         else rr.on = false;
 72         strcpy(rr.name,name);
 73         rr.mon = mon;
 74         rr.dd = dd;
 75         rr.hh = hh;
 76         rr.min = min;
 77         recordlist.push_back(rr);
 78     }
 79 
 80     sort(recordlist.begin(),recordlist.end(),cmp);
 81 
 82     for(i = 0 ;i < n ; i++)
 83     {
 84         if(recordlist[i].on && mm[recordlist[i].name].empty())
 85         {
 86             mm[recordlist[i].name].push_back(recordlist[i]);
 87         }
 88         else if ( recordlist[i].on && !mm[recordlist[i].name].empty() )
 89         {
 90             mm[recordlist[i].name].pop_back();
 91             mm[recordlist[i].name].push_back(recordlist[i]);
 92         }
 93         else if( !recordlist[i].on && !mm[recordlist[i].name].empty() )
 94         {
 95             int sum = 0;
 96             int time = 0;
 97 
 98             onecall one;
 99             one.mon = recordlist[i].mon;
100             one.bdd = mm[recordlist[i].name][0].dd;
101             one.bhh = mm[recordlist[i].name][0].hh;
102             one.bmin = mm[recordlist[i].name][0].min;
103             one.edd = recordlist[i].dd;
104             one.ehh = recordlist[i].hh;
105             one.emin = recordlist[i].min;
106 
107             //先把日期拿出来算了,否则会超时
108             if(recordlist[i].dd - mm[recordlist[i].name][0].dd > 1)
109             {
110                 time += (recordlist[i].dd - mm[recordlist[i].name][0].dd -1)*24*60;
111                 sum += (recordlist[i].dd - mm[recordlist[i].name][0].dd -1)*daycost;
112                 mm[recordlist[i].name][0].dd = recordlist[i].dd-1;
113             }
114 
115 
116             while (recordlist[i].dd != mm[recordlist[i].name][0].dd || recordlist[i].hh != mm[recordlist[i].name][0].hh || recordlist[i].min != mm[recordlist[i].name][0].min )
117             {
118                 ++time;
119                 ++mm[recordlist[i].name][0].min ;
120                 sum += rate[mm[recordlist[i].name][0].hh];
121                 if(mm[recordlist[i].name][0].min == 60)
122                 {
123                     mm[recordlist[i].name][0].min =0;
124                     mm[recordlist[i].name][0].hh++;
125                 }
126 
127                 if(mm[recordlist[i].name][0].hh == 24)
128                 {
129                     mm[recordlist[i].name][0].hh =0;
130                     mm[recordlist[i].name][0].dd++;
131                 }
132             }
133 
134 
135             one.time = time;
136             one.money = sum ;
137             user[recordlist[i].name].push_back(one);
138             mm[recordlist[i].name].pop_back();
139         }
140     }
141 
142     map<string,vector<onecall> >::iterator it;
143 
144     for( it = user.begin() ;it != user.end() ;it ++)
145     {
146         printf("%s %02d\n",it->first.c_str(),it->second[0].mon);
147         double tol = 0;
148         for(i= 0 ;i < it->second.size();i++)
149         {
150             printf("%02d:%02d:%02d %02d:%02d:%02d %d $%0.2lf\n",it->second[i].bdd,it->second[i].bhh,it->second[i].bmin,it->second[i].edd,it->second[i].ehh,it->second[i].emin,it->second[i].time,(double)it->second[i].money*1.0/100);
151             tol +=(double)it->second[i].money*1.0/100;
152         }
153 
154         printf("Total amount: $%0.2lf\n",tol);
155     }
156 
157     return 0;
158 }

 

1016. Phone Bills (25)

标签:

原文地址:http://www.cnblogs.com/xiaoyesoso/p/4302449.html

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