PAT甲级1016题Phone Bills求助(已解决)

发布于:2022-12-03 ⋅ 阅读:(691) ⋅ 点赞:(0)

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

代码长度限制

16 KB

时间限制

200 ms

内存限制

64 MB

 只A了一个样例捏

#include<algorithm>
#include<iostream>
#include<iomanip>
#include<utility>
#include<vector>
#include<map>
using namespace std;
int myStoi(string s) {//字符串转数字
	int res = 0;
	for (char ch : s) res = res * 10 + ch - '0';
	return res;
}
vector<int> cost_each_hour(24);//每小时的话费(美分/分钟)
class Moment {//时刻类
public:
	int month, day, hour, minute;
	bool state;//on-line为true,off-line为false
	string s;
	Moment(string s, string state) :s(s.substr(3, 8) + " ") {
		month = myStoi(s.substr(0, 2));
		day = myStoi(s.substr(3, 2));
		hour = myStoi(s.substr(6, 2));
		minute = myStoi(s.substr(9, 2));
		this->state = state == "on-line" ? true : false;
	}
};
pair<int, double> caculateMoney(Moment a, Moment b) {//计算相邻两个时刻的时间段的通话时长、话费
	double sumMoney = (60 - a.minute) * cost_each_hour[a.hour];
	sumMoney += b.minute * cost_each_hour[b.hour];
	int sumTime = 60 - a.minute + b.minute;
	for (int i = a.day; i <= b.day; i++) {
		for (int j = (i == a.day ? a.hour + 1 : 0); j < (i == b.day ? b.hour : 24); j++, sumTime += 60) {
			sumMoney += cost_each_hour[j] * 60;
		}
	}
	return make_pair(sumTime, sumMoney / 100);
}
bool cmp(Moment a, Moment b) {//比较两个时刻前后关系
	int ta = a.minute + 60 * a.hour + 24 * 60 * a.day;
	int tb = b.minute + 60 * b.hour + 24 * 60 * b.day;
	return ta < tb;
}
int main()
{
	for (int& cost : cost_each_hour) cin >> cost;
	map<string, vector<Moment>> map;
	int n;
	cin >> n;
	while (n--) {
		string name, s, state;
		cin >> name >> s >> state;
		map[name].push_back(Moment(s, state));
	}
	for (auto& it : map) {
		sort(it.second.begin(), it.second.end(), cmp);//对时刻进行排序
		n = it.second.size();
		vector<bool> valid(n, false);
		for (int i = 0; i < n - 1; i++) {
			if (it.second[i].state && !it.second[i + 1].state) {
				valid[i] = valid[i + 1] = true;//标记保留可用的两个时刻
			}
		}
		vector<Moment> replace;
		for (int i = 0; i < n; i++) {
			if (valid[i]) replace.push_back(it.second[i]);
		}
		it.second = replace;
	}
	const string Month[] = { "","01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
	for (auto& it : map) {
		cout << it.first << " "  << Month[it.second.front().month] << endl;
		double sum = 0.0;
		for (int i = 0; i < it.second.size(); i += 2) {
			pair<int, double> get = caculateMoney(it.second[i], it.second[i + 1]);
			sum += get.second;
			cout << it.second[i].s  << it.second[i + 1].s << get.first << " $" << fixed << setprecision(2) << get.second << endl;
		}
		cout << "Total amount: $" << fixed << setprecision(2) << sum << endl;
	}
}

看了一篇测试点的文章终于过了

#include<algorithm>
#include<iostream>
#include<iomanip>
#include<utility>
#include<vector>
#include<map>
using namespace std;
int myStoi(string s) {//字符串转数字
	int res = 0;
	for (char ch : s) res = res * 10 + ch - '0';
	return res;
}
vector<int> cost_each_hour(24);//每小时的话费(美分/分钟)
class Moment {//时刻类
public:
	int month, day, hour, minute;
	bool state;//on-line为true,off-line为false
	string s;
	Moment(string s, string state) :s(s.substr(3, 8) + " ") {
		month = myStoi(s.substr(0, 2));
		day = myStoi(s.substr(3, 2));
		hour = myStoi(s.substr(6, 2));
		minute = myStoi(s.substr(9, 2));
		this->state = state == "on-line" ? true : false;
	}
};
pair<int, double> caculateMoney(Moment a, Moment b) {//计算相邻两个时刻的时间段的通话时长、话费
	//同天同小时要考虑------------------------------------>>修改的地方
	if (a.day == b.day && a.hour == b.hour) return make_pair(b.minute - a.minute, (b.minute - a.minute) * cost_each_hour[a.hour] / 100.0);
	double sumMoney = (60 - a.minute) * cost_each_hour[a.hour];
	sumMoney += b.minute * cost_each_hour[b.hour];
	int sumTime = 60 - a.minute + b.minute;
	for (int i = a.day; i <= b.day; i++) {
		for (int j = (i == a.day ? a.hour + 1 : 0); j < (i == b.day ? b.hour : 24); j++, sumTime += 60) {
			sumMoney += cost_each_hour[j] * 60;
		}
	}
	return make_pair(sumTime, sumMoney / 100);
}
bool cmp(Moment a, Moment b) {//比较两个时刻前后关系
	int ta = a.minute + 60 * a.hour + 24 * 60 * a.day;
	int tb = b.minute + 60 * b.hour + 24 * 60 * b.day;
	return ta < tb;
}
int main()
{
	for (int& cost : cost_each_hour) cin >> cost;
	map<string, vector<Moment>> map;
	int n;
	cin >> n;
	while (n--) {
		string name, s, state;
		cin >> name >> s >> state;
		map[name].push_back(Moment(s, state));
	}
	for (auto& it : map) {
		sort(it.second.begin(), it.second.end(), cmp);//对时刻进行排序
		n = it.second.size();
		vector<bool> valid(n, false);
		for (int i = 0; i < n - 1; i++) {
			if (it.second[i].state && !it.second[i + 1].state) {
				valid[i] = valid[i + 1] = true;//标记保留可用的两个时刻
			}
		}
		vector<Moment> replace;
		for (int i = 0; i < n; i++) {
			if (valid[i]) replace.push_back(it.second[i]);
		}
		it.second = replace;
	}
	const string Month[] = { "","01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
	for (auto& it : map) {
		if (it.second.empty()) continue;//该顾客无消费-------------------->>修改的地方
		cout << it.first << " "  << Month[it.second.front().month] << endl;
		double sum = 0.0;
		for (int i = 0; i < it.second.size(); i += 2) {
			pair<int, double> get = caculateMoney(it.second[i], it.second[i + 1]);
			sum += get.second;
			cout << it.second[i].s  << it.second[i + 1].s << get.first << " $" << fixed << setprecision(2) << get.second << endl;
		}
		cout << "Total amount: $" << fixed << setprecision(2) << sum << endl;
	}
}

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到