PAT A1016 Phone Bills

 

1016 Phone Bills (25)(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.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.排序是预处理,可以使用map来映射<string,UserInfo> users 是来存放所以users的信息的

   2.其中数据结构有点多但是,慢慢做还是能在50分钟A掉这道题的。

   3.UserInfo的DS:

struct UserInfo{
	string name;
	vector<timeInfo> timeList;
	bool operator < (const UserInfo &B)const{
		return name<B.name;
	}
};

   其中name不用多少,虽然map本来的键值就是name的,但是在这里存储一个并不浪费。第二个是装timeIinfo的向量用来装每个人的时间表。最后重写< op 让map在插入时按照字典序来做。

struct timeInfo{
	string timeStr;
	int month,day,hor,min;
	string state;
	timeInfo(){
		month=0;day=0;hor=0;min=0;
	}
	bool operator < (const timeInfo &B)const{
		if(day != B.day)
			return day<B.day;
		else if(hor != B.hor)
			return hor<B.hor;
		else if(min != B.min)
			return min<B.min;
	}
};

   timeInfo的DS来存储每人的时间信息。这里主要要将string,化成月,日,时,分的int型。并且重写<。

void cutTstr(timeInfo &t){
	//int len = t.timeStr.length();
	t.month = 10*(t.timeStr[0]-'0')+t.timeStr[1] - '0';
	t.day = 10*(t.timeStr[3]-'0')+t.timeStr[4] - '0';
	t.hor = 10*(t.timeStr[6]-'0')+t.timeStr[7] - '0';
	t.min = 10*(t.timeStr[9]-'0')+t.timeStr[10] - '0';
}

  转换这里还是提一句,不要找“:”因为格式是定的,直接寻秩。

  接着最主要的是钱和时间怎么算?

void getMinMoey(timeInfo t1,timeInfo t2,double &m,int &ti){
	int cnt =0,cntM=0;
	double money =0.0;
	while(t1.day!=t2.day || t1.hor!=t2.hor ||t1.min!= t2.min){
		t1.min++;cnt++;cntM++;
		if(t1.min==60){
			money+=cntM*rateStructure[t1.hor];
			t1.min = 0;
			cntM = 0;
			t1.hor++;
			if(t1.hor == 24){
				t1.hor = 0;
				t1.day++;
			}
		}
	}
	m = money+rateStructure[t1.hor]*cntM;
	ti = cnt;
}

  这里的思路参考了算法笔记里的时间模拟那道题。注意最后的cntM在最后的那一个小时是未加入到总的钱里的。

  模块写完基本这道题就A了

  数据输入:

    int temp =0;
	for(int i=0;i<24;i++){
		scanf("%d",&temp);
		rateStructure[i] = temp;
	}
	int N=0;
	scanf("%d",&N);
	UserInfo tU;string ti,state;
	timeInfo tT;
	map<string , UserInfo> users;
	for(int i=0;i<N;i++){
		cin>>tU.name>>tT.timeStr>>tT.state;
		cutTstr(tT);
		//tU.timeList
		users[tU.name].name = tU.name;
		users[tU.name].timeList.push_back(tT);
	}

操作数据项:

for(map<string , UserInfo>::iterator it = users.begin(); it!= users.end();it++){
		sort(it->second.timeList.begin(),it->second.timeList.end());
		int minCnt=0;double money=0.0;double totalM=0.0;bool flag =false;
		for(int i=0;i<it->second.timeList.size()-1;i++){
			if(it->second.timeList[i].state=="on-line" && it->second.timeList[i+1].state=="off-line"){
				flag = true;
			}
		}
		if(flag){
			cout<<it->second.name<<' ';//<<it->second.timeList[0].month<<endl;
			printf("%02d\n",it->second.timeList[0].month);
			for(int i=0;i<it->second.timeList.size()-1;i++){
				minCnt=0,money=0.0;
				if(it->second.timeList[i].state=="on-line" && it->second.timeList[i+1].state=="off-line"){
					getMinMoey(it->second.timeList[i],it->second.timeList[i+1],money,minCnt);
					totalM+=money/100;
					cout<<it->second.timeList[i].timeStr.substr(3)<<' '<<it->second.timeList[i+1].timeStr.substr(3);
						//' '<<minCnt<<' '<<money<<endl;
					printf(" %d $%.2lf\n",minCnt,money/100);
				}
			}
			printf("Total amount: $%.2lf\n",totalM);
			}
	}

 这里注意,我是在最后的时候发现PAT的坑的。题目说的是 It is guaranteed that at least one call is well paired in the input.并非说所有用户至少有一个是配对,而是总的所有人至少有一个合法电话。所有如果该用户没有合法电话,则不打印他的信息。(但我觉得这样不好,没有合法的信息,打印¥0就行了呀,为什么直接不打印...)

最后AC源码:

#include<stdio.h>
#include<vector>
#include<string>
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
int rateStructure[24]={0};
struct timeInfo{
	string timeStr;
	int month,day,hor,min;
	string state;
	timeInfo(){
		month=0;day=0;hor=0;min=0;
	}
	bool operator < (const timeInfo &B)const{
		if(day != B.day)
			return day<B.day;
		else if(hor != B.hor)
			return hor<B.hor;
		else if(min != B.min)
			return min<B.min;
	}
};
void cutTstr(timeInfo &t){
	//int len = t.timeStr.length();
	t.month = 10*(t.timeStr[0]-'0')+t.timeStr[1] - '0';
	t.day = 10*(t.timeStr[3]-'0')+t.timeStr[4] - '0';
	t.hor = 10*(t.timeStr[6]-'0')+t.timeStr[7] - '0';
	t.min = 10*(t.timeStr[9]-'0')+t.timeStr[10] - '0';
}
struct UserInfo{
	string name;
	vector<timeInfo> timeList;
	bool operator < (const UserInfo &B)const{
		return name<B.name;
	}
};
void getMinMoey(timeInfo t1,timeInfo t2,double &m,int &ti){
	int cnt =0,cntM=0;
	double money =0.0;
	while(t1.day!=t2.day || t1.hor!=t2.hor ||t1.min!= t2.min){
		t1.min++;cnt++;cntM++;
		if(t1.min==60){
			money+=cntM*rateStructure[t1.hor];
			t1.min = 0;
			cntM = 0;
			t1.hor++;
			if(t1.hor == 24){
				t1.hor = 0;
				t1.day++;
			}
		}
	}
	m = money+rateStructure[t1.hor]*cntM;
	ti = cnt;
}
int main(){
	//rateStructure;
	int temp =0;
	for(int i=0;i<24;i++){
		scanf("%d",&temp);
		rateStructure[i] = temp;
	}
	int N=0;
	scanf("%d",&N);
	UserInfo tU;string ti,state;
	timeInfo tT;
	map<string , UserInfo> users;
	for(int i=0;i<N;i++){
		cin>>tU.name>>tT.timeStr>>tT.state;
		cutTstr(tT);
		//tU.timeList
		users[tU.name].name = tU.name;
		users[tU.name].timeList.push_back(tT);
	}
	for(map<string , UserInfo>::iterator it = users.begin(); it!= users.end();it++){
		sort(it->second.timeList.begin(),it->second.timeList.end());
		int minCnt=0;double money=0.0;double totalM=0.0;bool flag =false;
		for(int i=0;i<it->second.timeList.size()-1;i++){
			if(it->second.timeList[i].state=="on-line" && it->second.timeList[i+1].state=="off-line"){
				flag = true;
			}
		}
		if(flag){
			cout<<it->second.name<<' ';//<<it->second.timeList[0].month<<endl;
			printf("%02d\n",it->second.timeList[0].month);
			for(int i=0;i<it->second.timeList.size()-1;i++){
				minCnt=0,money=0.0;
				if(it->second.timeList[i].state=="on-line" && it->second.timeList[i+1].state=="off-line"){
					getMinMoey(it->second.timeList[i],it->second.timeList[i+1],money,minCnt);
					totalM+=money/100;
					cout<<it->second.timeList[i].timeStr.substr(3)<<' '<<it->second.timeList[i+1].timeStr.substr(3);
						//' '<<minCnt<<' '<<money<<endl;
					printf(" %d $%.2lf\n",minCnt,money/100);
				}
			}
			printf("Total amount: $%.2lf\n",totalM);
			}
	}
	
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值