首页 > 试题广场 >

回文日期

[编程题]回文日期
  • 热度指数:2449 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}一个日期用 \sf YYYYMMDD8 位数字表示,其中前四位为年份,接着两位为月份,最后两位为日期。

\hspace{15pt}若这 8 位数字本身是一个回文数,则称该日期为回文日期

\hspace{15pt}给定起始日期 a 与终止日期 b(均包含在内,且满足 a\leqq b),请计算区间内真实存在的回文日期数量。

\hspace{15pt}【名词解释】回文数:一个数字从左往右读与从右往左读完全相同。

\hspace{15pt}【日期合法性】
\hspace{23pt}\bullet\, 1,3,5,7,8,10,12 月有 31 天;
\hspace{23pt}\bullet\, 4,6,9,11 月有 30 天;
\hspace{23pt}\bullet\, 2 月在闰年29 天,平年有 28 天。

\hspace{15pt}【闰年判定】
\hspace{23pt}\bullet\, 能被 4 整除但不能被 100 整除的年份;
\hspace{23pt}\bullet\, 或能被 400 整除的年份。

输入描述:
\hspace{15pt}第一行输入 8 位整数 a,表示起始日期。
\hspace{15pt}第二行输入 8 位整数 b,表示终止日期。


输出描述:
\hspace{15pt}输出一个整数,表示 [a,b] 区间内回文日期的数量。
示例1

输入

20110101
20111231

输出

1

说明

\hspace{15pt}在这个样例中,在 \sf{20110101}\sf{20111231} 之间,回文日期有 1 个,即 \sf{20111102}
示例2

输入

20000101
20101231

输出

2

说明

\hspace{15pt}在这个样例中,在 \sf{20000101}\sf{20101231} 之间,回文日期有 2 个,即 \sf{20010002}\sf{20100102}
def is_runyear(x):
    if (x%4==0 and x%100!=0) or x%400==0:
        return True
    return False
days_of_month = [31,28,31,30,31,30,31,31,30,31,30,31]
a = input()
b = input()
year_a = int(a[0:4])
year_b = int(b[0:4])
month_start = int(a[4:6])
month_end = int(b[4:6])
day_start = int(a[6:8])
day_end = int(b[6:8])
count = 0
for year in range(year_a,year_b+1):
    reverse_year = str(year)[::-1]
    month = int(reverse_year[0:2])
    day = int(reverse_year[2:4])
    if year_a<year<year_b:
        if month==2:
            if is_runyear(year) and 1<=day<=29:
                count += 1
            elif not is_runyear(year) and 1<=day<=28:
                count += 1
        elif 1<=month<=12 and 1<=day<=days_of_month[month-1]:
            count += 1
    elif year_a==year<year_b:
        if month>month_start:
            if month==2:
                if is_runyear(year) and 1<=day<=29:
                    count += 1
                elif not is_runyear(year) and 1<=day<=28:
                    count += 1
            elif 1<=month<=12 and 1<=day<=days_of_month[month-1]:
                count += 1
        elif month==month_start:
            if month==2:
                if is_runyear(year) and day_start<=day<=29:
                    count += 1
                elif not is_runyear(year) and day_start<=day<=28:
                    count += 1
            elif 1<=month<=12 and day_start<=day<=days_of_month[month-1]:
                count += 1
    elif year_a<year==year_b:
        if month<month_end:
            if month==2:
                if is_runyear(year) and 1<=day<=29:
                    count += 1
                elif not is_runyear(year) and 1<=day<=28:
                    count += 1
            elif 1<=month<=12 and 1<=day<=days_of_month[month-1]:
                count += 1
        elif month==month_end:
            if 1<=day<=day_end:
                count += 1
    elif year_a==year==year_b:
        if month_start<month<month_end:
            if month==2:
                if is_runyear(year) and 1<=day<=29:
                    count += 1
                elif not is_runyear(year) and 1<=day<=28:
                    count += 1
            elif 1<=month<=12 and 1<=day<=days_of_month[month-1]:
                count += 1
        elif month_start==month<month_end:
            if month==2:
                if is_runyear(year) and day_start<=day<=29:
                    count += 1
                elif not is_runyear(year) and day_start<=day<=28:
                    count += 1
            elif 1<=month<=12 and day_start<=day<=days_of_month[month-1]:
                count += 1
        elif month_start<month==month_end:
            if 1<=day<=day_end:
                count += 1
        elif month_start==month==month_end:
            if day_start<=day<=day_end:
                count += 1

print(count)
发表于 2025-08-25 17:51:57 回复(0)