1016. Phone Bills (25)
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.10Totalamount:
12.10
CYLL 01
01:06:01 01:08:03 122
24.4028:15:4128:16:0524
3.85
Total amount:
28.25aaa0102:00:0104:23:594318
638.80
Total amount: $638.80
题意:
1.输入24个数,来表示24个时段打电话所需的费用(00:00-01:00 01:00-02:00等等依次至23:00-24:00)
2.输入一个N,接下来是N行记录,包括三个字段,用户名,日期时间(mm:dd:hh:mm),on-line/off-line
3.根据匹配的on-line和off-line的时间记录来计算用户产生的每次通话的费用及该月的总费用(忽略那些用户下一条记录不是off-line的记录,和上一条不是on-line的off-line记录)
思路:
1.使用结构体来接收每一条记录,按名字、时间来排序
2.注意跨天的时间的计算,我用的方法是模拟从开始日期时刻到结束日期时刻的循环计算
代码块
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int toll[24];
struct record
{
string name;//用户名
string date;//日期
string word;//on-line或是off-line
};
bool cmp(record a,record b)//定义排序的比较函数
{
if(a.name!=b.name)return a.name<b.name;
else return a.date<b.date;
}
int fee(int stime,int etime,int day)//计算每次通话产生的费用
{
int f=0;
for(int i=0;i<day+1;i++)//按天论转,至少一次,所以是日期差+1
{
for(int j=0;j<24;j++)
{
if(i==0&&(j+1)*60<stime)continue;//忽略开始时间前的月份
if(i==day&&(j+1)*60>=etime)//到了结束时间,返回费用
{
f+=(etime-stime)*toll[j];
return f;
}
f+=((j+1)*60-stime)*toll[j];
stime=(j+1)*60%1440;//开始时间变为该时段的结束时间,以便之后的计算,注意到23-24点时,stime会变成1440,但开始新的一天应该是0,所以要取模
// cout<<j<<" "<<f<<"\t";
}
int main()
{
int N;
for(int i=0;i<24;i++)
cin>>toll[i];
cin>>N;
record r[N],cur;
for(int i=0;i<N;i++)
{
cin>>r[i].name>>r[i].date>>r[i].word;
}
sort(r,r+N,cmp);
/* for(int i=0;i<N;i++)
cout<<r[i].name<< " "<<r[i].date<< " "<<r[i].word<<endl;*/
cur=r[0];
int total=0;
for(int i=1;i<N;i++)
{
if(r[i].name!=cur.name)//用户改变
{
cur=r[i];
//输出总费用
if(total>0)printf("Total amount: $%.2lf\n",(double)total/100);//cout<<"Total amount: $"<<total/100<<endl;
total=0;
continue;
}
if(cur.word=="on-line"&&r[i].word=="off-line")//匹配则计算,否则跳过
{
if(total==0)cout<<cur.name<<" "<<cur.date[0]<<cur.date[1]<<endl;
for(int j=3;j<11;j++)cout<<cur.date[j];
cout<<" ";
for(int j=3;j<11;j++)cout<<r[i].date[j];
cout<<" ";
int stime,etime;// 以分钟来计算
stime=((cur.date[6]-'0')*10+cur.date[7]-'0')*60+((cur.date[9]-'0')*10+cur.date[10]-'0');
etime=((r[i].date[6]-'0')*10+r[i].date[7]-'0')*60+((r[i].date[9]-'0')*10+r[i].date[10]-'0');
//cout<<"stime="<<stime<<" etime="<<etime<<" ";
//日期差
int day=((r[i].date[3]-'0')*10+r[i].date[4]-'0')-((cur.date[3]-'0')*10+cur.date[4]-'0');
cout<<etime-stime+day*1440<<" $";
int f=fee(stime,etime,day);
printf("%.2lf\n",(double)f/100);
//cout<<f/100<<endl;
total+=f;
cur=r[i];
}
else cur=r[i];
}
if(total!=0)printf("Total amount: $%.2lf\n",(double)total/100);
return 0;
}