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

PTA-1016——Phone Bills

时间:2019-01-23 13:03:43      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:ges   lock   osi   ecif   ted   case   red   minutes   color   

题目:

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-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<iostream>
  2 #include<cstring>
  3 #include<vector>
  4 #include<algorithm>
  5 using namespace std;
  6 int rate[24];
  7 int n;
  8 vector<string> namelist;
  9 struct Record{
 10     string name;
 11     int month;
 12     int day;
 13     int hour;
 14     int minute;
 15     bool line;
 16 }record[1001];
 17 
 18 struct Bill{
 19     int sday;
 20     int shour;
 21     int sminute;
 22     int eday;
 23     int ehour;
 24     int eminute;
 25 }bill[501];
 26 
 27 int minutes;
 28 float money;
 29 
 30 void insertName(string name){    //将名字按字母序排列,并清除相同名字 
 31     if(namelist.size()==0){
 32         namelist.push_back(name);
 33     }else if(namelist.size()==1){
 34         if(namelist[0]==name){
 35             return;
 36         }else if(namelist[0]<name){
 37             namelist.push_back(name);
 38         }else{
 39             namelist.insert(namelist.begin(),name);
 40         }
 41     }else{
 42         if(namelist.front()>name){
 43             namelist.insert(namelist.begin(),name);
 44         }else if(namelist.back()<name){
 45             namelist.push_back(name);
 46         }else{
 47             for(vector<string>::iterator it=namelist.begin();it!=namelist.end()-1;it++){
 48                 vector<string>::iterator nex=it+1;
 49                 if(name==*it||name==*nex){
 50                     return;
 51                 }else if(name>*it&&name<*nex){
 52                     namelist.insert(nex,name);
 53                     return;
 54                 }
 55             }
 56         }
 57     }
 58 }
 59 
 60 bool cmp(Record a,Record b){    //根据时间先后快排 
 61     int x=a.month*1000000+a.day*10000+a.hour*100+a.minute;
 62     int y=b.month*1000000+b.day*10000+b.hour*100+b.minute;
 63     if(x<y){
 64         return true;
 65     }else{
 66         return false;
 67     }
 68 }
 69 
 70 float calculateMoney(int sd,int sh,int sm,int ed,int eh,int em){    //计算时间与费用 
 71     if(ed-sd==0){    //当天开始当天结束 
 72         if(eh-sh==0){
 73             money=rate[sh]*(em-sm);
 74             minutes=em-sm;
 75         }else{
 76             minutes=60-sm+em+(eh-sh-1)*60;
 77             money=(60-sm)*rate[sh]+em*rate[eh];
 78             for(int i=sh+1;i<eh;i++){
 79                 money+=rate[i]*60;
 80             }
 81         }
 82     }else{        //当天开始,非当天结束 
 83         if(sh==23){
 84             money=rate[23]*(60-sm);
 85             minutes=60-sm;
 86         }else{
 87             minutes=60-sm+(23-sh)*60;
 88             money=(60-sm)*rate[sh];
 89             for(int i=sh+1;i<24;i++){
 90                 money+=rate[i]*60;
 91             }
 92         }
 93         if(eh==0){
 94             money+=rate[0]*em;
 95             minutes+=em;
 96         }else{
 97             minutes+=eh*60+em;
 98             money+=rate[eh]*em;
 99             for(int i=0;i<eh;i++){
100                 money+=rate[i]*60;
101             }
102         }
103         for(int i=sd+1;i<ed;i++){
104             minutes+=60*24;
105             for(int j=0;j<24;j++){
106                 money+=rate[j]*60;
107             }
108         }
109     }
110 }
111 
112 int main(){
113     for(int i=0;i<24;i++){
114         cin>>rate[i];
115     }
116     cin>>n;
117     for(int i=0;i<n;i++){
118         string l;
119         cin>>record[i].name;
120         insertName(record[i].name);
121         scanf("%d:%d:%d:%d",&record[i].month,&record[i].day,&record[i].hour,&record[i].minute);
122         cin>>l;
123         if(l=="on-line"){
124             record[i].line=true;
125         }else{
126             record[i].line=false;
127         }
128     }
129     sort(record,record+n,cmp);    //根据时间对记录快排 
130     for(vector<string>::iterator it=namelist.begin();it!=namelist.end();it++){    //根据姓名和月份,记录开始、结束的时间 
131         for(int i=1;i<=12;i++){
132             memset(bill,0,sizeof(bill));
133             int c=0;
134             bool on=true;
135             for(int j=0;j<n;j++){
136                 if(record[j].name==*it&&record[j].month==i&&on==true&&record[j].line==true){
137                     bill[c].sday=record[j].day;
138                     bill[c].shour=record[j].hour;
139                     bill[c].sminute=record[j].minute;
140                     on=false;
141                 }else if(record[j].name==*it&&record[j].month==i&&on==false&&record[j].line==true){
142                     bill[c].sday=record[j].day;
143                     bill[c].shour=record[j].hour;
144                     bill[c].sminute=record[j].minute;
145                 }else if(record[j].name==*it&&record[j].month==i&&on==false&&record[j].line==false){
146                     bill[c].eday=record[j].day;
147                     bill[c].ehour=record[j].hour;
148                     bill[c].eminute=record[j].minute;
149                     on=true;
150                     c++;
151                 }
152             }
153             if(c>0){        //开始输出结果 
154                 cout<<*it<<" ";
155                 if(i<10){
156                     cout<<"0"<<i<<endl;
157                 }else{
158                     cout<<i<<endl;
159                 }
160                 float sum=0.0;
161                 for(int j=0;j<c;j++){
162                     minutes=0;
163                     money=0.0;
164                     calculateMoney(bill[j].sday,bill[j].shour,bill[j].sminute,bill[j].eday,bill[j].ehour,bill[j].eminute);
165                     if(bill[j].sday<10){
166                         cout<<"0"<<bill[j].sday<<":";
167                     }else{
168                         cout<<bill[j].sday<<":"; 
169                     }
170                     if(bill[j].shour<10){
171                         cout<<"0"<<bill[j].shour<<":";
172                     }else{
173                         cout<<bill[j].shour<<":";
174                     }
175                     if(bill[j].sminute<10){
176                         cout<<"0"<<bill[j].sminute<<" ";
177                     }else{
178                         cout<<bill[j].sminute<<" ";
179                     }
180                     if(bill[j].eday<10){
181                         cout<<"0"<<bill[j].eday<<":";
182                     }else{
183                         cout<<bill[j].eday<<":"; 
184                     }
185                     if(bill[j].ehour<10){
186                         cout<<"0"<<bill[j].ehour<<":";
187                     }else{
188                         cout<<bill[j].ehour<<":";
189                     }
190                     if(bill[j].eminute<10){
191                         cout<<"0"<<bill[j].eminute<<" ";
192                     }else{
193                         cout<<bill[j].eminute<<" ";
194                     }
195                     printf("%d $%.2f\n",minutes,money/100);
196                     sum+=money/100;
197                 }
198                 printf("Total amount: $%.2f\n",sum);
199             }
200         }
201     }
202     return 0;
203 }

 

 

PTA-1016——Phone Bills

标签:ges   lock   osi   ecif   ted   case   red   minutes   color   

原文地址:https://www.cnblogs.com/orangecyh/p/10308245.html

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