【Python】1016 Phone Bills
https://pintia.cn/problem-sets/994805342720868352/problems/994805493648703488
真的酸q,题目长,涉及时间处理
其中测试点1,2一直无法过的原因居然是月份取成日期了,崩溃
def tran_min(s: str) -> int:
ls = list(map(int, s.split(":")))
t = ls[1] * 24 * 60 + ls[2] * 60 + ls[3]
return t
def cal_cost(t1: str, t2: str, Bill: list) -> tuple:
time = 0
cost = 0
d1, h1, m1 = tuple(map(int, t1.split(":")))
d2, h2, m2 = tuple(map(int, t2.split(":")))
# Day 跨天的情况计算
cost += (d2 - d1) * sum(Bill) * 60
time += (d2 - d1) * 1440
# H h1h2不用这样分大小,懒得想了,这样符合直觉思维
if h1 < h2:
cost += Bill[h1] * (60 - m1) + Bill[h2] * m2 + sum(
[Bill[i] * 60 for i in range(h1 + 1, h2)])
time += (60 - m1) + m2 + 60 * (h2 - h1 - 1)
elif h1 == h2:
cost += Bill[h1]*(m2 - m1)
time += m2 - m1
elif h1 > h2:
cost -= Bill[h2] * (60 - m2) + Bill[h1] * m1 + sum(
[Bill[i] * 60 for i in range(h2 + 1, h1)])
time -= (60 - m2) + m1 + 60 * (h1 - h2 - 1)
return (time, cost / 100)
Bill = list(map(int, input().split()))
data = []
for _ in range(int(input())):
data.append(input().split())
# 按时间排序
data = sorted(data, key=lambda x: tran_min(x[1]))
# 用户去重+排序
member = sorted(list(set([i[0] for i in data])), key=lambda x: x)
# 建立用户状态字典
status = {}
# 建立用户账单字典
cost = {}
for i in data:
i_member = i[0]
i_time = i[1]
i_ststus = i[2]
if status.get(i_member, False) == False:
# 初始化字典
status.setdefault(i_member, [i_time, i_ststus])
cost.setdefault(i_member, [])
continue
if status[i_member][1] == 'on-line' and i_ststus == 'off-line':
# 此种情况计费
detail = []
detail.append(status[i_member][0][3:])
detail.append(i_time[3:])
cost[i_member].append(detail)
status[i_member][0] = i_time
status[i_member][1] = i_ststus
elif status[i_member][1] == 'on-line' and i_ststus == 'on-line':
# 重新计时
status[i_member][0] = i_time
elif status[i_member][1] == 'off-line':
# 重设时间
status[i_member][0] = i_time
status[i_member][1] = i_ststus
# 打印
# 没有账单的用户不输出
# 为了防止同一时间接电话+挂电话没有产生话费,此类情况也不输出
for i in member:
if cost[i] == [] : #CHECK1
continue
else:
# CHECK2
cost_print = []
total_cost=0
for data in cost[i]:
m, c = cal_cost(data[0], data[1], Bill)
cost_print.append(
(data[0], str(data[1]), str(m), "$" + "{0:.2f}".format(c)))
total_cost += c
if total_cost!=0:
M = data[0][1][0:2]
print(i, M)
for j in cost_print:
print(" ".join(j))
print("Total amount: ${0:.2f}".format(total_cost))

浙公网安备 33010602011771号