蓝桥杯 回文日期【第十一届】

以为没有任何技巧,看了大佬的代码才知道是有的。。不过这题数据很小也无所谓了。

大佬思路:直接枚举年份 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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值