蓝桥杯回文日期判断

思想:对于回文数的判断方法,最快的就是取其中一半的字符串长度,为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)
相关推荐
捷瑞电子工坊8 天前
嵌入式蓝桥杯从零点亮第一个LED
stm32·蓝桥杯·cubemx·嵌入式·led·ll·锁存
斯内科13 天前
验证回文数Palindromic Number
回文数
aqiu11111118 天前
【数位 DP / 记忆化搜索】蓝桥云课 - 光轨区的 AI 数 题解
c++·算法·蓝桥杯·数位dp
aqiu11111120 天前
【并查集 / 图论】蓝桥云课 - 魔法大陆的城市群(求无向图连通块数量)题解
蓝桥杯·图论
aqiu11111120 天前
【LeetCode 902】最大为 N 的数字组合 - 详细题解与数位组合思路
数据结构·算法·leetcode·蓝桥杯·数位dp
Chester_199922 天前
CSP202312C.树上搜索
开发语言·数据结构·c++·蓝桥杯·stl
CoderYanger23 天前
A.每日一题:3622. 判断整除性
java·程序人生·算法·leetcode·面试·职场和发展·蓝桥杯
Chester_199924 天前
CSP202303C.LDAP
开发语言·c++·蓝桥杯·stl
Chester_19991 个月前
CSP202206C.角色授权
开发语言·数据结构·c++·蓝桥杯
Chester_19991 个月前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯