题目描述
2020 年春节期间,有一个特殊的日期引起了大家的注意:2020 年 2 月 2 日。因为如果将这个日期按 “yyyymmdd” 的格式写成一个 8 位数是 20200202,恰好是一个回文数。我们称这样的日期是回文日期。有人表示 20200202 是 “千年一遇” 的特殊日子。对此小明很不认同,因为不到 2 年之后就是下一个回文日期:20211202 即 2021 年 12 月 2 日。也有人表示 20200202 并不仅仅是一个回文日期,还是一个 ABABBABA 型的回文日期。对此小明也不认同,因为大约 100 年后就能遇到下一个 ABABBABA 型的回文日期:21211212 即 2121 年 12 月 12 日。算不上 “千年一遇”,顶多算 “千年两遇”。给定一个 8 位数的日期,请你计算该日期之后下一个回文日期和下一个 ABABBABA 型的回文日期各是哪一天。
输入描述
输入包含一个八位整数 NN,表示日期。
对于所有评测用例,10000101≤N≤89991231,保证 NN 是一个合法日期的 8 位数表示。
输出描述
输出两行,每行 1 个八位数。第一行表示下一个回文日期,第二行表示下一个 ABABBABA 型的回文日期。
题目分析:
首先,看懂题目要求,需要输出下一个回文日期,以及下一个ABABBABA型回文日期,那么这里我们需要注意,需要输出的不是回文数,是回文日期,所以要对数字进行限定,保证输出的数是合法数据。在判断一个数据是否是日期时,要特别注意二月的天数,也就是需要判断是否是闰年。
#include<stdio.h>
int next_huiwendate(int date)
{
while (date >= 10000101 && date <= 89991231)
{
date++;
//这里的judge1用于判断数据是否是回文数
//judge2用于判断是否是日期,当二者同时满足时,即为回文日期
int judge1 = 0;
int judge2 = 0;
int year = date / 10000;
int month = date % 10000 / 100;
int day = date % 100;
int umonth = (month % 10) * 10 + month / 10;
int uday = (day % 10) * 10 + day / 10;
int Month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if ((year % 4 == 0 && year % 100 != 0) || year / 400 == 0)
Month[2] = 29;
//判断是否是回文数
if (year / 100 == uday && year % 100 == umonth)
judge1 = 1;
//判断该数据是否是日期
if (month <= 12 && day <= Month[month])
judge2 = 1;
if (judge1 == 1 && judge2 == 1)
{
return date;
}
}
}
//ABABBABA
int AB_huiwen(int date)
{
while (date >= 10000101 && date <= 89991231)
{
date++;
int judge1 = 0;
int judge2 = 0;
int year = date / 10000;
int month = date % 10000 / 100;
int day = date % 100;
int umonth = (month % 10) * 10 + month / 10;
int uday = (day % 10) * 10 + day / 10;
int Month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if ((year % 4 == 0 && year % 100 != 0) || year / 400 == 0)
Month[2] = 29;
//判断是否是ababbaba回文数
if (year / 100 == uday && year % 100 == umonth && year / 100 == year % 100 && month == day)
judge1 = 1;
//判断该数据是否是日期
if (month <= 12 && day <= Month[month])
judge2 = 1;
if (judge1 == 1 && judge2 == 1)
{
return date;
}
}
}
int main()
{
int date;
scanf("%d", &date);
int a = next_huiwendate(date);
printf("%d\n", a);
int b = AB_huiwen(date);
printf("%d\n", b);
return 0;
}