描述

每个日期可以转成8位数字,比如 2018年5月12日 对应的就是 20180512。小明发现,自己的生日转成8位数字后,8个数字都没有重复,而且自他出生之后到今天,再也没有8位数字都不重复的日子了。请问小明的生日是哪天?‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

输入格式

该题目没有输入‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

输出格式

小明的生日是月*日

  1. def birthdate(current_date):
  2. """接收一个表示日期的字符串为参数,依次向前检验每一个合法的日期是否符合题目要求,以规定格式返回符合条件的日期。"""
  3. while True:
  4. current_date = current_date - 1 # 从当前日期向前逐日判定
  5. str_day = str(current_date) # 整型转字符串
  6. if int(str_day[4:6]) > 13:
  7. continue # 忽略月份超过12的数字
  8. elif str_day[4:6] in ['01', '03', '05', '07', '08', '10', '12'] and int(str_day[-2:]) > 31:
  9. continue # 以上月份,忽略超过31的日期
  10. elif str_day[4:6] in ['04', '06', '09', '11'] and int(str_day[-2:]) > 30:
  11. continue # 以上月份,忽略超过30的日期
  12. elif str_day[4:6] in ['02'] and int(str_day[-2:]) > 19:
  13. continue # 2月忽略超过19的日期,20以后有重复数字
  14. elif len(set(str_day)) == 8: # 字符串转集合,判断无重复数字是否8个
  15. return str_day # 找到第一个满足条件的日期中止循环 19870625
  16. if __name__ == '__main__':
  17. today = int(input()) # 输入当前日期
  18. birthday = birthdate(today)
  19. print(f'出生日期是{birthday[:4]}年{birthday[4:6]}月{birthday[6:8]}日') # 出生日期是1987年06月25日
  1. def leap(year):
  2. if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
  3. return True
  4. else:
  5. return False
  6. def birthdate(current_date):
  7. """接收一个表示日期的字符串为参数,依次向前检验每一个合法的日期是否符合题目要求,以规定格式返回符合条件的日期。"""
  8. for year in range(int(current_date[: 4]), 1900, -1):
  9. for month in range(12, 0,-1):
  10. if month in [1, 3, 5, 7, 8, 10, 12]:
  11. num_day = 31
  12. elif month in [4, 6, 9, 11]:
  13. num_day = 30
  14. elif month == 2 and leap(year):
  15. num_day = 29
  16. else:
  17. num_day = 28
  18. for day in range(num_day,0,-1):
  19. date = str(year) + f'{month:02}' + f'{day:02}'
  20. if len(set(date)) == 8 and int(date)<=int(current_date):
  21. return date
  22. if __name__ == '__main__':
  23. today = input() # 输入日期
  24. birthday = birthdate(today)
  25. print(f'出生日期是{birthday[:4]}年{birthday[4:6]}月{birthday[6:8]}日') # 出生日期是1987年06月25日
  1. def leap(year):
  2. if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
  3. return True
  4. else:
  5. return False
  6. def birthdate(current_date):
  7. """接收一个表示日期的字符串为参数,依次向前检验每一个合法的日期是否符合题目要求,以规定格式返回符合条件的日期。"""
  8. 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}
  9. for year in range(int(current_date[: 4]), 1900, -1):
  10. for month in range(12, 0, -1):
  11. if month == 2 and leap(year):
  12. num_day = day_of_month[month] + 1
  13. else:
  14. num_day = day_of_month[month]
  15. for day in range(num_day, 0, -1):
  16. date = str(year) + f'{month:02}' + f'{day:02}'
  17. if len(set(date)) == 8 and int(date) <= int(current_date):
  18. return date
  19. if __name__ == '__main__':
  20. today = input() # 输入日期
  21. birthday = birthdate(today)
  22. print(f'出生日期是{birthday[:4]}年{birthday[4:6]}月{birthday[6:8]}日') # 出生日期是1987年06月25日