以为没有任何技巧,看了大佬的代码才知道是有的。。不过这题数据很小也无所谓了。
大佬思路:直接枚举年份 a
,将其翻转得到 b
,那么 a + b
就是一个回文数。
#include <iostream>
#include <string>
#include <sstream>
bool valid_date(int date);
bool palidrome_date(std::string date);
bool ABABBABA(std::string date);
bool flag = true;
int month_days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int main(void)
{
int date;
std::string str_date;
std::cin >> date;
while (true)
{
++date;
if (valid_date(date))
{
std::stringstream ss;
ss << date;
ss >> str_date;
if (palidrome_date(str_date))
{
if (flag == true)
{
std::cout << date<<std::endl;
flag = false;
}
if (ABABBABA(str_date))
{
std::cout << date;
break;
}
}
}
}
return 0;
}
bool valid_date(int date)
{
int year, month, day;
year = date / 10000;
month = date % 10000 / 100;
day = date % 100;
if (month <= 12)
{
if (year % 4 == 0 && year % 100 != 0 || year % 100 == 0)
month_days[2] = 29;
else
month_days[2] = 28;
if (day <= month_days[month])
return true;
}
return false;
}
bool palidrome_date(std::string date)
{
for (int a = 0, b = 7; a <= 3; ++a, --b)
{
if (date[a] != date[b])
return false;
}
return true;
}
bool ABABBABA(std::string date)
{
if (date[0] == date[2] && date[1] == date[3])
return true;
else
return false;
}