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

解答:该题我花了相当多的时间,总结起来,一是自己没有用好STL库,比如如何存储一个用户的多条记录,可以用map来简化代码,否则会有多个变量来控制过程,降低了程序的可读性;二是思维的局限性,比如存储一对记录,前一个的status为on-line,后一个为off-line,且这两个的name相同,这时可直接判定即可;三是对于费用的计算上,可以分别计算一对记录的费用,然后求差,这样考虑的因素就少了很多。总之,现在想来,这题思路还是蛮清晰,关键在于方法正确。

AC代码如下:

#include<cstdio>
#include<string>
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<map>

using namespace std;

int fees[25];  //记录收费 

typedef struct{
	char name[25];
	int month, day, hour, min, time;  //用time记录总的min数 
	int status;
}record;

//用isFormer定义排序算法 
bool isFormer(record r1, record r2){
	if(strcmp(r1.name, r2.name) != 0){
		return (strcmp(r1.name, r2.name) < 0);
	}else{
		return r1.time < r2.time;
	}
}
//返回从00:00:00 - DD:HH:MM的money 
int getMoney(record r)
{
	double total = fees[r.hour]*r.min + fees[24]*60*r.day;
	for(int i = 0; i < r.hour; ++i)
		total += fees[i]*60;
	return total;
}
int main()
{
	int N; 
	vector<record> records;
	
	//记录电话收费费用 
	for(int i = 0; i < 24; ++i){
		scanf("%d", &fees[i]);
		fees[24] += fees[i];
	}	
	//记录通话记录 
	scanf("%d", &N);
	for(int i = 0; i < N; ++i){
		record tmp; 
		char status[15];
		scanf("%s %d:%d:%d:%d %s", tmp.name, &tmp.month, &tmp.day, &tmp.hour, &tmp.min, status);
		if(strcmp(status,"on-line") == 0) tmp.status = 0;
		else tmp.status = 1;
		tmp.time = tmp.day * 24 * 60 + tmp.hour * 60 + tmp.min;
		records.push_back(tmp);
	}
	sort(records.begin(), records.end(), isFormer);
	//用outputs来存储要输出的记录,该步将坏记录删掉
	map<string, vector<record> > outputs; 
	
	for(int i = 1; i < records.size(); ++i){
		if(records[i].status == 1 && records[i-1].status == 0 && strcmp(records[i].name, records[i-1].name) == 0){
			string name = string(records[i].name);
			outputs[name].push_back(records[i-1]);
			outputs[name].push_back(records[i]);
		}
	} 
	//输出结果
	for(map<string, vector<record> >::iterator iter = outputs.begin(); iter != outputs.end(); ++iter)
	{
		double total = 0, money = 0;
		//输出名字和月份 
		vector<record> tmp( (iter->second).begin(), (iter->second).end() );
		printf("%s %02d\n", (iter->first).c_str(), tmp[0].month);
		
		for(int i = 0; i < tmp.size(); i += 2)
		{
			//输出时间信息 
			printf("%02d:%02d:%02d %02d:%02d:%02d ", tmp[i].day, tmp[i].hour, tmp[i].min, tmp[i+1].day, tmp[i+1].hour, tmp[i+1].min);
			int mins = tmp[i+1].time - tmp[i].time;
			printf("%d ", mins);
			int money = 0;  //记录cents 
			money = getMoney(tmp[i+1]) - getMoney(tmp[i]);
		
			printf("$%.2f\n", 0.01 * money);
		
			total += 0.01 * money;
		}
		//输出总的amount 
		printf("Total amount: $%.2f\n", total);	
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值