在其他博客上看到一道 "2018阿里巴巴在线编程题"
我记得题目大概是这样的:输入数据n(n>=6)代表n个数,从0-9任意输入n个数,然后在n个数选出6个数xx-xx-xx表示 时:分:秒,求出最小时间和最大时间,若时间无效,则输出时间无效,例如:
输入:
6
6 5 4 3 2 1
输出:
123456 235641输入:
6
9 9 9 9 9 9
输出:
时间无效
看着有点意思,就自己做了一遍,输出格式自己美化了一下,没有严格按照题目要求。代码如下:
def find_min_in_range(l, lb, ub): if lb > l[-1] or ub < l[0]: return -1 for x in l[:]: if x >= lb: l.remove(x) return x def find_max_in_range(l, lb, ub): if ub < l[-1] or lb > l[0]: return -1 for x in l[:]: if x <= ub: l.remove(x) return x l0 = [3, 5, 1, 7, 4, 2, 9, 9] # l0 = [9, 5, 1, 7, 4, 2, 9, 9] l1 = l0[:] l1.sort() l2 = l1[:] l2.reverse() min_h0 = find_min_in_range(l1, 0, 2) if min_h0 == 2: min_h1 = find_min_in_range(l1, 0, 3) else: min_h1 = find_min_in_range(l1, 0, 9) min_m0 = find_min_in_range(l1, 0, 5) min_m1 = find_min_in_range(l1, 0, 9) min_s0 = find_min_in_range(l1, 0, 5) min_s1 = find_min_in_range(l1, 0, 9) max_h0 = find_max_in_range(l2, 0, 2) if max_h0 == 2: max_h1 = find_max_in_range(l2, 0, 3) else: max_h1 = find_max_in_range(l2, 0, 9) max_m0 = find_max_in_range(l2, 0, 5) max_m1 = find_max_in_range(l2, 0, 9) max_s0 = find_max_in_range(l2, 0, 5) max_s1 = find_max_in_range(l2, 0, 9) if -1 in [min_h0, min_h1, min_m0, min_m1, min_s0, min_s1, max_h0, max_h1, max_m0, max_m1, max_s0, max_s1]: print(‘invalid data‘) else: print("min clock: %s%s:%s%s:%s%s" % (min_h0, min_h1, min_m0, min_m1, min_s0, min_s1)) print("max clock: %s%s:%s%s:%s%s" % (max_h0, max_h1, max_m0, max_m1, max_s0, max_s1))
输出:
min clock: 12:34:57
max clock: 23:59:49
原文地址:https://www.cnblogs.com/parody/p/10002258.html
时间: 2024-10-02 18:01:06