题目详情
天梯图书阅览室请你编写一个简单的图书借阅统计程序。当读者借书时,管理员输入书号并按下
S
键,程序开始计时;当读者还书时,管理员输入书号并按下E
键,程序结束计时。书号为不超过1000的正整数。当管理员将0作为书号输入时,表示一天工作结束,你的程序应输出当天的读者借书次数和平均阅读时间。注意:由于线路偶尔会有故障,可能出现不完整的纪录,即只有
S
没有E
,或者只有E
没有S
的纪录,系统应能自动忽略这种无效纪录。另外,题目保证书号是书的唯一标识,同一本书在任何时间区间内只可能被一位读者借阅。
输入格式
输入在第一行给出一个正整数N(≤10),随后给出N天的纪录。每天的纪录由若干次借阅操作组成,每次操作占一行,格式为:
书号
([1, 1000]内的整数)键值
(S
或E
)发生时间
(hh:mm
,其中hh
是[0,23]内的整数,mm
是[0, 59]内整数)每一天的纪录保证按时间递增的顺序给出。
输出格式
对每天的纪录,在一行中输出当天的读者借书次数和平均阅读时间(以分钟为单位的精确到个位的整数时间)。
输入样例
3
1 S 08:10
2 S 08:35
1 E 10:00
2 E 13:16
0 S 17:00
0 S 17:00
3 E 08:10
1 S 08:20
2 S 09:00
1 E 09:20
0 E 17:00
输出样例
2 196
0 0
1 60
代码展示
class Book:
def __init__(self, book_no):
self.book_no = book_no
#属性有4项:书号,借阅次数,借阅分钟数(累计),借书开始时间
self.borrow_times = 0
self.borrow_minutes = 0
self.borrow_start = -1 # -1表示没有借书
def set_borrow_start(self, start):
self.borrow_start = start # 有可能覆盖前面的借书行为
def set_borrow_end(self, end):
if self.borrow_start == -1:
return #没有匹配的借书行为,忽略
self.borrow_times += 1
self.borrow_minutes += end - self.borrow_start
self.borrow_start = -1
def get_borrow_times(self):
return self.borrow_times
def get_borrow_minutes(self):
return self.borrow_minutes
#读取所有天数的借阅记录,返回借阅记录列表
def get_next_day(days):
records=[]
for k in range(days):
records.append([])
i=0
while True:
book_no, s_e, moment = input().split()
book_no = int(book_no)
if book_no == 0:
i+=1
if i == days:
break
continue
hour, minute = [int(s) for s in moment.split(':')]
moment = hour * 60 + minute
records[i].append((book_no, s_e, moment))
return records
# 统计一天的借阅记录,返回借阅次数和平均分钟数
def handle_records(records):
borrow_times = 0
total_minutes = 0
books = [Book(i) for i in range(1001)]
# print(books[1].book_no, books[1].borrow_times, books[1].borrow_minutes, books[1].borrow_start)
# print(books[1000].book_no, books[1000].borrow_times, books[1000].borrow_minutes, books[1000].borrow_start)
for record in records:
if record[1] == 'S':
books[record[0]].set_borrow_start(record[2])
elif record[1] == 'E':
books[record[0]].set_borrow_end(record[2])
for i in range(1, 1001):
borrow_times += books[i].borrow_times
total_minutes += books[i].borrow_minutes
if borrow_times == 0:
avg_minutes = 0
else:
avg_minutes = int(round(total_minutes / borrow_times, 0))
return borrow_times, avg_minutes
days = int(input())
record=get_next_day(days)
# print(record)
for d in range(days):
if len(record[d]) == 0:
print(0, 0)
continue
borrow_times, avg_minutes = handle_records(record[d])
print(borrow_times, avg_minutes)