題目:python
輸入數據n(n>=6)表明n個數,從0-9任意輸入n個數,而後在n個數選出6個數xx-xx-xx表示 時:分:秒,求出最小時間和最大時間,若時間無效,則輸出時間無效,例如:
輸入:
6
6 5 4 3 2 1
輸出:
12:34:56 23:56:41app
輸入:
6
999999
輸出:
時間無效dom
import random def min_max_date(n): if n < 6: return "valid n" nums = [] for i in range(n): nums.append(int(random.random() * 10)) mind = min_date(nums) maxd = max_date(nums) print mind, maxd def min_date(nums): nums = sorted(nums) print nums if nums[0] > 2: return "valid date" if nums[1] > 3: return "valid date" h_1 = nums[0] h_2 = nums[1] nums = nums[2:] if nums[0] > 5: return "valid date" m_1 = nums[0] nums = nums[1:] if nums[1] > 5: m_2 = nums[1] nums.pop(1) else: m_2 = nums[0] nums.pop(0) if nums[0] > 5: return "valid date" s_1 = nums[0] s_2 = nums[1] return "%s%s:%s%s:%s%s" % (h_1, h_2, m_1, m_2, s_1, s_2) def max_date(nums): h_1 = gen(0, 2, nums) if h_1 is None: return "valid date" h_2 = gen(0, 3, nums) if h_2 is None: return "valid date" m_1 = gen(0, 5, nums) if m_1 is None: return "valid date" m_2 = gen(0, 9, nums) if m_2 is None: return "valid date" s_1 = gen(0, 5, nums) if s_1 is None: return "valid date" s_2 = gen(0, 9, nums) if s_2 is None: return "valid date" return "%s%s:%s%s:%s%s" % (h_1, h_2, m_1, m_2, s_1, s_2) def gen(s, e, nums): n = None for i in range(len(nums)): num = nums[i] if s <= num <= e: n = num else: nums.pop(i - 1) break return n min_max_date(10)
其實最小值也可按求最大值方式取,客戶們本身動手吧code