Subtask #1没过 猜测是范围判断出了问题,求大佬修改

P2010 [NOIP2016 普及组] 回文日期

Chen_Yu_Wei @ 2024-08-10 14:54:06

范围判断函数:

#判断月日是否在范围内
def fanwei(i):
    i = int(i)
    if i>=int(s1_3) and i<=int(s2_3):
        return True
    else:
        return False

完整代码:

s1_1 = input()
s2_1 = input()
s = 0

#切片年份
s1_2 = s1_1[0:4]
s2_2 = s2_1[0:4]

#切片月日
s1_3 = s1_1[4:8]
s2_3 = s2_1[4:8]

#判断是否为合法日期
def hefa(s):
    s1 = s[0:2]
    s2 = s[2:4]
    if (len(s1) != 2) or (len(s2) != 2):
        return False
    if (s1 == '01') or (s1 == '02') or (s1 == '03') or (s1 == '04') or (s1 == '05') or (s1 == '06') or (s1 == '07') or (s1 == '08') or (s1 == '09') or (s1 == '10') or (s1 == '11') or (s1 == '12'):
        pass
    else:
        return False
    if int(s[3::-1])%4 == 0:
        if (s1 == '02'):
            if(int(s2) > 29):
                return False
        elif (s1 == '04') or (s1 == '06') or (s1 == '09') or (s1 == '11'):
            if(int(s2) > 30):
                return False
        else:
            if (int(s2) > 31):
                return False
    else:
        if (s1 == '02'):
            if(int(s2) > 28):
                return False
        elif (s1 == '04') or (s1 == '06') or (s1 == '09') or (s1 == '11'):
            if(int(s2) > 30):
                return False
        else:
            if (int(s2) > 31):
                return False
    return True

#判断月日是否在范围内
def fanwei(i):
    i = int(i)
    if i>=int(s1_3) and i<=int(s2_3):
        return True
    else:
        return False

#循环所有年份
for i in range(int(s1_2),int(s2_2)+1):
    #以年份倒序获取回文月日
    i = str(i)[3::-1]
    if hefa(i) and fanwei(i):
        s += 1

print(s)

|