PAT A1016 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.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

代码实现和思路

/*
以前的PAT系统的个别题是不严谨的 这道题就是 题目默认每个人在每一天是打一通电话
这个隐含条件没有告诉 如果误以为一个人一天会打多个电话 反而加大的处理难度
选择用map容器来存储每个人的通话记录单独处理最好
*/

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <fstream>
using namespace std;
struct node {
	string id,status;
	int MM,DD,HH,SS,times;
};
map<string,vector<node>> mp;
vector<node> sq;
int toll[24],n;
bool cmp(node a,node b) {
	if(a.id!=b.id) return a.id<b.id;
	else return a.times<b.times;
}

//计算每个人一天内一通电话的时间
double countVal(vector<node> &ans,int idx1,int idx2) {
	if(ans[idx1].times==ans[idx2].times) return 0;
	double total=0;
	int times=ans[idx1].times;//设置初始值就是接通电话时间
	int cnt=0;
	while(1) {
	//这里计算话费的方式我推荐采用我的形式 相比较网上方式更加方便该解法不同判断跨天问题 无脑累加即可
		total+=toll[times/60%24];//计算每分钟的话费  
		times++;
		if(times==ans[idx2].times) break;//分钟累加到通话结束为止
	}
	return total;
}

int main() {
	for(int i=0; i<24; i++) scanf("%d",&toll[i]);
	cin>>n;
	string id,status;
	int MM,DD,HH,SS,times;
	while(n--) {
		cin>>id;
		scanf("%d:%d:%d:%d",&MM,&DD,&HH,&SS);
		times=DD*24*60+HH*60+SS;
		cin>>status;
		sq.push_back(node {id,status,MM,DD,HH,SS,times});
	}
	sort(sq.begin(),sq.end(),cmp);
	for(int i=0; i<sq.size()-1; i++) {
		if(sq[i].id==sq[i+1].id&&sq[i].status=="on-line"&&sq[i+1].status=="off-line") {//筛选出符合要求的一组数据保存下来
			mp[sq[i].id].push_back(sq[i]);
			mp[sq[i].id].push_back(sq[i+1]);
		}
	}
	for(auto it : mp) {
		printf("%s %02d\n",it.first.c_str(),it.second[0].MM);
		vector<node> ans=it.second;
		double dayCost,total;
		dayCost=total=0;
		//单独计算每一组数据 题默认每人不同的天里只会打一通电话
		for(int i=0; i<ans.size()-1; i+=2) {
			dayCost=countVal(ans,i,i+1);
			total+=dayCost;
			printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",ans[i].DD,ans[i].HH,ans[i].SS
			       ,ans[i+1].DD,ans[i+1].HH,ans[i+1].SS,ans[i+1].times-ans[i].times,dayCost/100);
		}
		printf("Total amount: $%.2f\n",total/100);
	}
	return 0;
}
posted @ 2021-01-20 20:48  coderJ_ONE  阅读(65)  评论(0编辑  收藏  举报