Run ID:84421
提交时间:2024-07-16 08:57:33
from datetime import datetime, timedelta def days_until_18th_birthday(birthdate_str): # 将字符串日期转换为datetime对象 birthdate = datetime.strptime(birthdate_str, '%Y-%m-%d') # 计算18岁生日的日期 # 如果18岁生日已经过了,就计算下一个18岁生日的日期 target_birthday = birthdate.replace(year=birthdate.year + 18) # 如果18岁生日在2月29日,并且出生年份是闰年,但18岁生日那年不是闰年,则需要调整日期 if birthdate.month == 2 and birthdate.day == 29 and not target_birthday.month == 2 or target_birthday.day == 29: target_birthday = target_birthday.replace(day=28) # 计算到达18岁生日的天数 days_count = (target_birthday - birthdate).days # 如果天数为负数,说明没有18岁生日(比如2月29日出生,但18岁那年不是闰年) if days_count < 0: return -1 return days_count # 读取输入的测试用例数量 T = int(input()) # 对每个测试用例进行处理 for _ in range(T): birthdate_str = input().strip() print(days_until_18th_birthday(birthdate_str))