描述
每个日期可以转成8位数字,比如 2018年5月12日 对应的就是 20180512。小明发现,自己的生日转成8位数字后,8个数字都没有重复,而且自他出生之后到今天,再也没有8位数字都不重复的日子了。请问小明的生日是哪天?
输入格式
该题目没有输入
输出格式
小明的生日是年月*日
def birthdate(current_date):
"""接收一个表示日期的字符串为参数,依次向前检验每一个合法的日期是否符合题目要求,以规定格式返回符合条件的日期。"""
while True:
current_date = current_date - 1 # 从当前日期向前逐日判定
str_day = str(current_date) # 整型转字符串
if int(str_day[4:6]) > 13:
continue # 忽略月份超过12的数字
elif str_day[4:6] in ['01', '03', '05', '07', '08', '10', '12'] and int(str_day[-2:]) > 31:
continue # 以上月份,忽略超过31的日期
elif str_day[4:6] in ['04', '06', '09', '11'] and int(str_day[-2:]) > 30:
continue # 以上月份,忽略超过30的日期
elif str_day[4:6] in ['02'] and int(str_day[-2:]) > 19:
continue # 2月忽略超过19的日期,20以后有重复数字
elif len(set(str_day)) == 8: # 字符串转集合,判断无重复数字是否8个
return str_day # 找到第一个满足条件的日期中止循环 19870625
if __name__ == '__main__':
today = int(input()) # 输入当前日期
birthday = birthdate(today)
print(f'出生日期是{birthday[:4]}年{birthday[4:6]}月{birthday[6:8]}日') # 出生日期是1987年06月25日
def leap(year):
if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
return True
else:
return False
def birthdate(current_date):
"""接收一个表示日期的字符串为参数,依次向前检验每一个合法的日期是否符合题目要求,以规定格式返回符合条件的日期。"""
for year in range(int(current_date[: 4]), 1900, -1):
for month in range(12, 0,-1):
if month in [1, 3, 5, 7, 8, 10, 12]:
num_day = 31
elif month in [4, 6, 9, 11]:
num_day = 30
elif month == 2 and leap(year):
num_day = 29
else:
num_day = 28
for day in range(num_day,0,-1):
date = str(year) + f'{month:02}' + f'{day:02}'
if len(set(date)) == 8 and int(date)<=int(current_date):
return date
if __name__ == '__main__':
today = input() # 输入日期
birthday = birthdate(today)
print(f'出生日期是{birthday[:4]}年{birthday[4:6]}月{birthday[6:8]}日') # 出生日期是1987年06月25日
def leap(year):
if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
return True
else:
return False
def birthdate(current_date):
"""接收一个表示日期的字符串为参数,依次向前检验每一个合法的日期是否符合题目要求,以规定格式返回符合条件的日期。"""
day_of_month = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
for year in range(int(current_date[: 4]), 1900, -1):
for month in range(12, 0, -1):
if month == 2 and leap(year):
num_day = day_of_month[month] + 1
else:
num_day = day_of_month[month]
for day in range(num_day, 0, -1):
date = str(year) + f'{month:02}' + f'{day:02}'
if len(set(date)) == 8 and int(date) <= int(current_date):
return date
if __name__ == '__main__':
today = input() # 输入日期
birthday = birthdate(today)
print(f'出生日期是{birthday[:4]}年{birthday[4:6]}月{birthday[6:8]}日') # 出生日期是1987年06月25日