回文日期(蓝桥杯)

该程序实现从整数中提取每一位,并用于检查日期的有效性和特定序列。`checkSati`函数验证日期是否合法,考虑闰年情况;`check1`和`check2`函数分别检查整数的每一位是否成对出现。程序通过迭代找到满足条件的日期并输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

int 类型取出各位的方法:

“ % ”除掉前面的," % "后面的时剩下的几位;
“ / ”除掉后面的 用来取一个int整数的各个位置

eg:对于数abcde
a=abcde/10000
b=(abcde/1000)%10
c=(abcde/100)%10
d=(abcde/10)%10
e=(abcde/1)%10

#include<iostream>
#include<map>
#include<math.h>
using namespace std;
//“% ”除掉前面的,%后面的时剩下的几位“/ ”除掉后面的 用来取一个int整数的各个位置 

int months[20]={0,31,28,31,30,31,30,31,31,30,31,30,31};

bool checkSati(int date)
{
	int year=date/10000;
	int month=date%10000/100;
	int day=date%100;
	if(month>12||month<1) return false;
	if(months[month]<day&&month!=2) return false;
	if(month==2)
	{
		if((year%4==0&&year%100!=0)||year%400==0)
		{
			if(day>29)
			return false;
		} 
		else
		{
			if(day>28)
			return false;
		}  
	}
	return true;
}

bool check1(int date)
{
	int a,b,c,d,e,f,g,h;
	a=date/10000000;
	b=(date/1000000)%10;
	c=(date/100000)%10;
	d=(date/10000)%10;
	e=(date/1000)%10;
	f=(date/100)%10;
	g=(date/10)%10;
	h=(date/1)%10;
	if(a==h&&b==g&&c==f&&d==e)
    {
        return true;
    }
    return false;

}

bool check2(int date)
{
	int a,b,c,d,e,f,g,h;
	a=date/10000000;
	b=(date/1000000)%10;
	c=(date/100000)%10;
	d=(date/10000)%10;
	e=(date/1000)%10;
	f=(date/100)%10;
	g=(date/10)%10;
	h=(date/1)%10;
	if(a==h&&a==c&&c==f&&b==g&&b==d&&d==e)
    {
        return true;
    }
    return false;

}
int main()
{
	int date,flag=0;
	cin>>date;
	for(int i=date+1;;i++)
	{
		if(checkSati(i))
		{
			if(!flag&&check1(i))
			{
				cout<<i<<endl;
				flag=1;
			}
			if(check2(i))
			{
				cout<<i<<endl;
				return 0;
			}
		}
		
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值