蓝桥杯回文日期判断

思想:对于回文数的判断方法,最快的就是取其中一半的字符串长度,为s,然后将其进行翻转为s' ,再把两者进行拼接即可保证是回文数,这样子就解决了枚举所有回文数的问题。

注意点:

  • 要求必须是有效日期
  • 注意闰年的2月份问题
    代码:
    (1)判断所给字符串是不是回文数
    (a) 取得前一半的数据,将原来一半和翻转后的一半进行拼接即可确保是回文数
bash 复制代码
    //transformer to string
    string s= to_string(num),t= to_string(num);
    //翻转其中一个
    reverse(t.begin() ,t.end());
    s+=t;//S一定为回文数

(b)判断日期是不是有效的

bash 复制代码
    int y= stoi(s.substr(0,4)),m=stoi(s.substr(4,2)),d= stoi(s.substr(6,2));//取出年月日
    if(y%400==0||(y%4==0&&y%100!=0))//如果是闰年
        month[1]=29;
    else
        month[1]=28;
    //接着判断月份是否超过12
    if(m<1||m>12)
        return "-1";
    else
        return s;

全部代码:

cpp 复制代码
string check1(int num)
{

    //transformer to string
    string s= to_string(num),t= to_string(num);
    //翻转其中一个
    reverse(t.begin() ,t.end());
    s+=t;
    int y= stoi(s.substr(0,4)),m=stoi(s.substr(4,2)),d= stoi(s.substr(6,2));//取出年月日
    if(y%400==0||(y%4==0&&y%100!=0))//如果是闰年
        month[1]=29;
    else
        month[1]=28;
    //接着判断月份是否超过12
    if(m<1||m>12)
        return "-1";
    else
        return s;
}

常见函数:

to_string()//int转为字符串
reverse()//字符翻转
substr(0,4)//0是指起点位置,4是指复制4个,即从0号字符开始,复制四个作为返回值

(2)判断是不是ABABBABA类型的回文数

cpp 复制代码
//判断是否是ABABBABA类型的回文数
string  check2(int num)
{
    //transformer to string
    string s= to_string(num),t= to_string(num);
    //翻转其中一个
    reverse(t.begin() ,t.end());
    s+=t;

    if(s[0]==s[2]&&s[1]==s[3])
        return s;
    else
        return "-1";

}

整个项目工程如下:

huiwen.cpp

cpp 复制代码
// Created by HP on 2024/1/7.
//判断是否是回文数
#include "huiwen_number.h"

int date;
int month[12]={31,28,31,30,31,30,31,30,31,30,31,30};//初始化每月天数
/*------------------- 判断是否是一个回文数---------------------
 * 判断回文数,只需要判断前半部分是不是回文数,然后翻转拼接即可
 *
 *
*/

//判断日期是否合法
string check1(int num)
{

    //transformer to string
    string s= to_string(num),t= to_string(num);
    //翻转其中一个
    reverse(t.begin() ,t.end());
    s+=t;
    int y= stoi(s.substr(0,4)),m=stoi(s.substr(4,2)),d= stoi(s.substr(6,2));//取出年月日
    if(y%400==0||(y%4==0&&y%100!=0))//如果是闰年
        month[1]=29;
    else
        month[1]=28;
    //接着判断月份是否超过12
    if(m<1||m>12)
        return "-1";
    else
        return s;
}
//判断是否是ABABBABA类型的回文数
string  check2(int num)
{
    //transformer to string
    string s= to_string(num),t= to_string(num);
    //翻转其中一个
    reverse(t.begin() ,t.end());
    s+=t;

    if(s[0]==s[2]&&s[1]==s[3])
        return s;
    else
        return "-1";

}

huiwen.h

cpp 复制代码
//
// Created by HP on 2024/1/7.
//
#include <iostream>
#include "string"
using namespace std;
#include "algorithm"
#ifndef HUIWEN_NUMBER_H
#define HUIWEN_NUMBER_H
extern int date;
extern int month[12];//初始化每月天数
string check1(int num);
string  check2(int num);
#endif //CHAPTER1_HUIWEN_NUMBER_H

main.cpp

cpp 复制代码
#include <iostream>
#include "huiwen_number.h"
using namespace std;

int main()
{
string  ans1="";
/* -------------------判断是否是回文数的蓝桥杯题目---------------------*/
cout<<"请你输入一个日期"<<endl;
cin>> date;//date回文.h文件中有定义了
for(int i=date/10000;;i++)
{
    if(check1(i)=="-1"||check1(i)== to_string(date))
        continue;
    else
    {
        if(ans1=="")
            ans1= check1(i);
        if(check2(i)!="-1")
        {
            cout<<ans1<<"\n"<<check2(i)<<endl;
            break;
        }

    }

}
    return 0;


}

CMakelist.txt

xml 复制代码
cmake_minimum_required(VERSION 3.26)
project(chapter1)

set(CMAKE_CXX_STANDARD 17)

add_executable(chapter1 main.cpp
        huiwen_number.cpp
        huiwen_number.h)
相关推荐
CV金科8 小时前
蓝桥杯-STM32G431RBT6(串口)
c语言·stm32·单片机·嵌入式硬件·蓝桥杯
weixin_446260858 小时前
24年蓝桥杯及攻防世界赛题-MISC-1
网络安全·蓝桥杯
abments10 小时前
蓝桥杯DS18B20程序源码
职场和发展·蓝桥杯
CV金科1 天前
蓝桥杯-STM32G431RBT6(解决LCD与LED引脚冲突的问题)
c语言·stm32·单片机·嵌入式硬件·蓝桥杯
—你的鼬先生3 天前
蓝桥杯单片机STC15F2K60S2第十二届省赛代码详细讲解(附完整代码)
单片机·蓝桥杯·stc15f2k60s2
清风千里.4 天前
蓝桥杯备赛---1.安装软件 配置环境
蓝桥杯
DANGAOGAO6 天前
蓝桥杯1.确定字符串是否包含唯一字符
python·蓝桥杯
2401_858286117 天前
E32.【C语言 】练习:蓝桥杯题 懒羊羊字符串
c语言·开发语言·蓝桥杯
Keep striving7 天前
C语言蓝桥杯:语言基础
c语言·开发语言·算法·蓝桥杯
TNT_JQR9 天前
蓝桥杯嵌入式国三备赛经验分享
经验分享·嵌入式硬件·蓝桥杯