门店每月要将门店的水电费发票邮寄回总部,为了提升管理效率,总部财务同学希望统计一下每月预计会在哪些日期段收到指定区域的发票,比如8月份预计收到发票的日期如下
输入: 2-5,6-7,8-9
输出: 2-9
思路:分类讨论
line = input().split(",")
A = []
for s in line:
a = s.split("-")
A.append([int(a[0]), int(a[1])])
A.sort(key=lambda x : x[0])
s, e = A[0][0], A[0][1]
ans = []
for i in range(1, len(A)):
if A[i][0] <= e + 1:
e = max(e, A[i][1])
if i == len(A)-1:
ans.append(str(s) + '-' + str(e))
else:
ans.append(str(s) + '-' + str(e))
s = A[i][0]
e = A[i][1]
res = ",".join(ans)
print(res)