Pat【甲级】 1016 Phone Bills 电话账单(附中文题目) 模拟

这一系列是为了准备2021年3月的pat甲级,目标是把pat甲级的150多道题目刷完并写相应题解,对一些在做题过程中相对来说花了较多时间的题目会进行单独的题解,而对于较简单的会进行多题放在一篇博客中,希望这些总结能够帮助到大家,预祝大家在pat比赛中取得好成绩

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

中文题目

长途电话公司按以下规则向客户收费: 拨打长途电话每分钟要花费一定的费用,具体收费取决于拨打电话的时间。 客户开始拨打长途电话的时间将被记录,客户挂断电话的时间也将被记录。 每个月都要给客户发送一次话费账单,账单中应包含每次通话记录以及相关收费等信息。 给定一组电话记录,你的工作是为客户准备帐单。
输入格式
输入包含两部分:费率结构和电话记录。
费率结构由一行组成,该行包含24个非负整数,分别表示从 00:00-01:00 的收费(分/分钟),从 01:00-02:00 的收费,以此类推…
下一行包含一个正整数 N。
接下来 N 行,每行包含一条记录。
每个记录由客户名称(最多 20 个字符的字符串,不带空格),时间和日期(mm:dd:hh:mm)以及单词 on-line 或 off-line 组成。
所有日期都在同一个月内,每个 on-line 记录都与按时间顺序排列的同一位客户的下一条记录配对,但前提是这条记录是 off-line。
所有未与 off-line 记录配对的 on-line 记录以及未与 on-line 记录配对的 off-line 记录都必须忽略。
输入中至少包含一个成功的配对。
同一客户在同一时间不会有两个或以上的电话记录。
使用 24 小时制记录时间。

输出格式
你需要为每个客户打印电话费。
账单必须按照客户姓名的字母顺序(按ASCII码顺序,大写字母在前,小写字母在后)打印。
对于每个客户,首先以示例显示的格式在一行中打印客户名称和帐单月份。
然后,对于每个通话时间段,在一行中分别打印开始和结束时间和日期(dd:hh:mm),持续时间(以分钟为单位)和通话费用。
通话必须按时间顺序列出。
最后,以示例显示的格式打印该月的总费用。                                              
注意,没有任何有效通话记录的客户直接忽略,不予打印账单。

这一题刚刚做的时候也是没什么思路,自己写了一些代码发现原来的思路有点乱虽然也通过了,但是感觉没什么非常清晰的思路,后来看了y总的代码,茅塞顿开(๑•̀ㅂ•́)و✧思路就非常清晰了

思路

接下来就看我的思路吧:

1、首先先要把题目看清楚:
 - 它说的时如果按照时间的顺序排序如果没有相匹配的状态那么就忽略,
	意思就是:只有on-line和off-line才能够匹配,像on-line-----on-line这样的数据就直接忽略
 - 如果在这个月账单为0则什么都不打印(包括姓名、月份以及总价钱)

2、要想清楚用什么数据结构来存储我们的通话记录和姓名

3、怎么处理通话记录中的时间能够更好更快的计算出时间

4、把每个人的通话记录单独取出进行匹配,并计算通话费用

数据存储


对于时间的计算我们可以用一个前缀和数组来存储这样既能提高我们的代码可读性还能够提高时间效率
前缀和计算的是我们当前时间到这个月一号0:0所要花费的费用

//cost[i]表示在第i这个时间段的话费
for (int i = 1; i <= N; i++) {
	//(i-1)%1440/60表示的是到这一
	sum[i] = sum[i - 1] + cost[(i - 1) % 1440 / 60] / 100.0;
}

在这要特殊提一下:为什么是i-1:
在这里插入图片描述


对于存储姓名以及通话记录我这用到了map的映射

//用来保存通话记录
struct Person{
	//和前缀和的思想一样计算的是到当月1号0:0的分钟数
	int minute;
	string state;
	//因为后面输出的时候要格式化输出所以我们就可以把这个保存下来等下就很方便
	string format_time;
	bool operator<(const Person& t)const {
		return minute < t.minute;
	}
};
//姓名以及通话记录
map<string, vector<Person>>mm;

实现代码

#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
#include<string>
#include<cstdio>
using namespace std;
const int N = 31 * 1440;
int n;
int cost[24];
double sum[N];
struct Person{
	int minute;
	string state;
	string format_time;
	bool operator<(const Person& t)const {
		return minute < t.minute;
	}
};
map<string, vector<Person>>mm;

int main() {
	for (int i = 0; i < 24; i++) {
		cin >> cost[i];
	}
	for (int i = 1; i <= N; i++) {
		sum[i] = sum[i - 1] + cost[(i - 1) % 1440 / 60] / 100.0;
	}
	cin >> n;
	int mouth, day, hour, minute;
	for (int i = 0; i < n; i++) {
		char name[25],state[25];
		
		//格式化输入
		scanf("%s %d:%d:%d:%d %s", name, &mouth, &day, &hour, &minute, state);
		char format_time[25];
		//按照格式把数据输入到format_time中
		sprintf(format_time, "%02d:%02d:%02d", day, hour, minute);
		mm[name].push_back({ (day - 1) * 1440 + hour * 60 + minute, state,format_time});
	}

	for (auto& p : mm) {
		double res = 0;
		auto t = p.second;
		sort(t.begin(), t.end());
	
		for (int i = 0; i < t.size()-1; i++) {
			if (t[i].state == "on-line" && t[i + 1].state == "off-line") {
			    //进行判断,如果一直没有这一项那么姓名以及账单都不会被打印
			    if(res==0){
		            printf("%s %02d\n",p.first.c_str(),mouth);
			    }
			    //累加花费
				res += sum[t[i + 1].minute] - sum[t[i].minute];
				
				//要用%s输出string类型的字符串时记得一定要.c_str()
				printf("%s %s %d $%.2lf\n",t[i].format_time.c_str(),t[i + 1].format_time.c_str()
				                          ,t[i+1].minute-t[i].minute,sum[t[i + 1].minute] - sum[t[i].minute]);
			}
		}
		
		if(res!=0)//如果res不为0那么输出
		printf("Total amount: $%.2lf\n", res);
	}

	return 0;
}

运行结果

在这里插入图片描述
希望这篇文章对你有帮助,谢谢(●’◡’●)!

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
账单信息管理系统是一个用于记录和管理账单信息的应用程序。使用Python语言开发这样一个系统是很常见的。 在Python中,你可以使用各种库和框架来实现账单信息管理系统。以下是一个简单的示例,展示了如何使用Python编写一个基本的账单信息管理系统: ```python class Bill: def __init__(self, bill_id, amount, date): self.bill_id = bill_id self.amount = amount self.date = date class BillManager: def __init__(self): self.bills = [] def add_bill(self, bill): self.bills.append(bill) def remove_bill(self, bill_id): for bill in self.bills: if bill.bill_id == bill_id: self.bills.remove(bill) break def get_total_amount(self): total_amount = 0 for bill in self.bills: total_amount += bill.amount return total_amount def get_bills_by_date(self, date): bills_by_date = [] for bill in self.bills: if bill.date == date: bills_by_date.append(bill) return bills_by_date # 示例用法 bill_manager = BillManager() bill1 = Bill(1, 100.0, '2022-01-01') bill2 = Bill(2, 200.0, '2022-01-02') bill3 = Bill(3, 300.0, '2022-01-02') bill_manager.add_bill(bill1) bill_manager.add_bill(bill2) bill_manager.add_bill(bill3) total_amount = bill_manager.get_total_amount() print(f"Total amount: {total_amount}") bills_by_date = bill_manager.get_bills_by_date('2022-01-02') print("Bills on '2022-01-02':") for bill in bills_by_date: print(f"Bill ID: {bill.bill_id}, Amount: {bill.amount}") ``` 以上示例代码定义了一个`Bill`类,用于表示账单对象。`BillManager`类则负责管理所有账单对象,包括添加、删除、获取总金额和按日期获取账单等功能。 这只是一个简单的示例,实际的账单信息管理系统可能需要更多的功能和数据处理。你可以根据自己的需求进行扩展和修改。希望这能帮到你!如果你有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值