蓝桥杯练习系统
日期问题
题目描述
小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在 1960 年 1 月 1 日至 2059 年 12 月 31 日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。
更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。
比如 02/03/04,可能是 2002 年 03 月 04 日、2004 年 02 月 03 日或 2004 年 03 月 02 日。
给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?
a, b, c = map(str,input().split('/'))
aint = int(a)
bint = int(b)
cint = int(c)
arr = []
if aint >= 60 and aint <= 99 and bint >= 1 and bint <= 12 and cint >= 1 and cint <= 31:
arr.append('19' + a + '-' + b + '-' + c)
elif aint >= 0 and aint <= 59 and bint >= 1 and bint <= 12 and cint >= 1 and cint <= 31:
arr.append('20' + a + '-' + b + '-' + c)
if aint >= 1 and aint <= 12 and bint >= 1 and bint <= 31 and cint >= 60 and cint <= 99:
arr.append('19' + c + '-' + a + '-' + b)
elif aint >= 1 and aint <= 12 and bint >= 1 and bint <= 31 and cint >= 0 and cint <= 59:
arr.append('20' + c + '-' + a + '-' + b)
if aint >= 1 and aint <= 31 and bint >= 1 and bint <= 12 and cint >= 60 and cint <= 99:
arr.append('19' + c + '-' + b + '-' + a)
elif aint >= 1 and aint <= 31 and bint >= 1 and bint <= 12 and cint >= 0 and cint <= 59:
arr.append('20' + c + '-' + b + '-' + a)
arr = sorted(list(set(arr)))
num = len(arr)
from datetime import datetime
for i in range(num):
try:
answer = datetime.strptime(arr[i], "%Y-%m-%d")
# 格式化日期对象为 '年-月-日' 格式的字符串
print(answer.strftime("%Y-%m-%d"))
except ValueError:
continue