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

题意就是根据几条包含用户,时间及是否是on-line或off-line的记录,以及对应时间段的话费,来计算每个用户的账单。根据题意,主要是先要把记录进行排序,我先用一个结构体来保存一条记录,然后用sort来对记录先按名字再按时间先后进行排序。因为有无效记录,所以要使on-line与off-line 一一对应,把在on-line前输入的off-line都舍去,遇到off-line时取之前最近的on-line,然后再根据读入的时间,计算相应的金额,并最终相加,代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;
struct Record {
	string id;
	string date;
	bool isOnline;
};

int tolls[24];

bool compara(const Record a,const Record b)
{
	if (a.id < b.id)
		return true;
	else if (a.id == b.id)
	{
		if (a.date <= b.date)
			return true;
		else
			return false;
	}
	else
		return false;
}

int calculateCharge(string start, string end)
{
	int charge = 0;
	int day1 = atoi(start.substr(0, 2).c_str());
	int day2 = atoi(end.substr(0, 2).c_str());
	int hour1 = atoi(start.substr(3, 5).c_str());
	int hour2 = atoi(end.substr(3, 5).c_str());
	int minute1 = atoi(start.substr(6, 8).c_str());
	int minute2 = atoi(end.substr(6, 8).c_str());
	while (day1 < day2)
	{
		charge += (60 - minute1) * tolls[hour1];
		minute1 = 0;
		if (hour1 == 23)
		{
			hour1 = 0;
			day1++;
		}
		else
			hour1++;
	}
	while (hour1 < hour2)
	{
		if (hour1 + 1 == hour2)
		{
			charge += (60 - minute1) * tolls[hour1] + minute2 * tolls[hour2];
			hour1 = hour2;
			minute1 = minute2;
		}
		else
		{
			charge += (60 - minute1)*tolls[hour1];
			hour1++;
			minute1 = 0;
		}
	}
	if (minute1 < minute2)
	{
		charge += (minute2 - minute1) * tolls[hour1];
	}

	return charge;

}
int main()
{
	vector<Record>records;
	Record temp;
	string s;
	int N;
	for (int i = 0;i < 24;i++)
	{
		cin >> tolls[i];
	}
	cin >> N;
	for (int i = 0;i < N;i++)
	{
		cin >> temp.id;
		cin >> temp.date;
		cin >> s;
		if (s == "on-line")
			temp.isOnline = true;
		else
			temp.isOnline = false;
		records.push_back(temp);
	}

	sort(records.begin(), records.end(), compara);
	
	int charge;
	double sum = 0;
	int minute;
	int customer;
	int i = 0;
	while (i < records.size())
	{
		customer = i;
		cout << records[customer].id << " " << records[customer].date[0] << records[customer].date[1] << endl;
		while (records[i].isOnline == false)	//一开始直接是off-line则是无效的记录
		{
			i++;
		}
		while (i < records.size() && records[i].id == records[customer].id)   //同一个用户都在这个循环中处理
		{
			while (i < records.size() && records[i].id == records[customer].id && records[i].isOnline == true)
			{
				customer = i;
				i++;
			}
			if (i < records.size() && records[i].id == records[customer].id && records[i].isOnline == false)
			{
				cout << records[customer].date.substr(3, 11) << " " << records[i].date.substr(3, 11) << " ";
				minute = (atoi(records[i].date.substr(3, 5).c_str()) - atoi(records[customer].date.substr(3, 5).c_str())) * 24 * 60
					+ (atoi(records[i].date.substr(6, 8).c_str()) - atoi(records[customer].date.substr(6, 8).c_str())) * 60
					+ (atoi(records[i].date.substr(9, 11).c_str()) - atoi(records[customer].date.substr(9, 11).c_str()));
				cout << minute << " $";
				charge = calculateCharge(records[customer].date.substr(3, 11), records[i].date.substr(3, 11));
				cout << fixed << setprecision(2) << (double)charge / 100.0 << endl;
				sum += (double)charge / 100.0;
				i++;
			}
		}
		cout << "Total amount: $" << sum << endl;
		sum = 0;
	}
	
	return 0;
}

这样的做法只对了用例0:

后来去查阅了网上的资料。发现首先如果该用户一条正确记录都没有,应该不输出,而不是像我之前的那样输出总金额为0。然后我在处理on-line后两个off-line的时候按我的代码会把两次记录都输出,所以应该把false那段的循环加入到下面一个顾客的大循环中,这样修改后代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;
struct Record {
	string id;
	string date;
	bool isOnline;
};

int tolls[24];

bool compara(const Record a,const Record b)
{
	if (a.id < b.id)
		return true;
	else if (a.id == b.id)
	{
		if (a.date <= b.date)
			return true;
		else
			return false;
	}
	else
		return false;
}

int calculateCharge(string start, string end)
{
	int charge = 0;
	int day1 = atoi(start.substr(0, 2).c_str());
	int day2 = atoi(end.substr(0, 2).c_str());
	int hour1 = atoi(start.substr(3, 5).c_str());
	int hour2 = atoi(end.substr(3, 5).c_str());
	int minute1 = atoi(start.substr(6, 8).c_str());
	int minute2 = atoi(end.substr(6, 8).c_str());
	while (day1 < day2)
	{
		charge += (60 - minute1) * tolls[hour1];
		minute1 = 0;
		if (hour1 == 23)
		{
			hour1 = 0;
			day1++;
		}
		else
			hour1++;
	}
	while (hour1 < hour2)
	{
		if (hour1 + 1 == hour2)
		{
			charge += (60 - minute1) * tolls[hour1] + minute2 * tolls[hour2];
			hour1 = hour2;
			minute1 = minute2;
		}
		else
		{
			charge += (60 - minute1)*tolls[hour1];
			hour1++;
			minute1 = 0;
		}
	}
	if (minute1 < minute2)
	{
		charge += (minute2 - minute1) * tolls[hour1];
	}

	return charge;

}
int main()
{
	vector<Record>records;
	Record temp;
	string s;
	int N;
	for (int i = 0;i < 24;i++)
	{
		cin >> tolls[i];
	}
	cin >> N;
	for (int i = 0;i < N;i++)
	{
		cin >> temp.id;
		cin >> temp.date;
		cin >> s;
		if (s == "on-line")
			temp.isOnline = true;
		else
			temp.isOnline = false;
		records.push_back(temp);
	}

	sort(records.begin(), records.end(), compara);
	
	int charge;
	double sum = 0;
	int minute;
	int customer;
	int i = 0;
	bool k = false;
	while (i < records.size())
	{
		customer = i;
		while (i < records.size() && records[i].id == records[customer].id)   //同一个用户都在这个循环中处理
		{
			while (i < records.size() && records[i].isOnline == false)	//一开始直接是off-line则是无效的记录
			{
				i++;
			}

			while (i < records.size() && records[i].id == records[customer].id && records[i].isOnline == true)
			{
				customer = i;
				i++;
			}
			if (i < records.size() && records[i].id == records[customer].id && records[i].isOnline == false)
			{
				if (!k)
				{
					cout << records[customer].id << " " << records[customer].date[0] << records[customer].date[1] << endl;
					k = true;
				}	
				cout << records[customer].date.substr(3, 11) << " " << records[i].date.substr(3, 11) << " ";
				minute = (atoi(records[i].date.substr(3, 5).c_str()) - atoi(records[customer].date.substr(3, 5).c_str())) * 24 * 60
					+ (atoi(records[i].date.substr(6, 8).c_str()) - atoi(records[customer].date.substr(6, 8).c_str())) * 60
					+ (atoi(records[i].date.substr(9, 11).c_str()) - atoi(records[customer].date.substr(9, 11).c_str()));
				cout << minute << " $";
				charge = calculateCharge(records[customer].date.substr(3, 11), records[i].date.substr(3, 11));
				cout << fixed << setprecision(2) << (double)charge / 100.0 << endl;
				sum += (double)charge / 100.0;
				i++;
			}
		}
		if(k)
			cout << "Total amount: $" << sum << endl;
		sum = 0;
		k = false;
	}
	
	return 0;
}

这样修改后就通过了全部用例。这题还是比较复杂的。


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值