【C++】回文日期

题目

在日常生活中,通过年、月、日这三个要素可以表示出一个唯一确定的日期。

牛牛习惯用 8 位数字表示一个日期,其中,前 4 位代表年份,接下来 2 位代表月份,最后 2 位代表日期。

显然:一个日期只有一种表示方法,而两个不同的日期的表示方法不会相同。

牛牛认为,一个日期是回文的,当且仅当表示这个日期的 8 位数字是回文的。

现在,牛牛想知道:在他指定的两个日期之间(包含这两个日期本身),有多少个真实存在的日期是回文的。

一个 8 位数字是回文的,当且仅当对于所有的 i(1≤i≤8) 从左向右数的第 i 个数字和第 9−i 个数字(即从右向左数的第 i 个数字)是相同的。

例如:

对于 2016 年 11 月 19 日,用 8 位数字 20161119 表示,它不是回文的。
对于 2010 年 1 月 2 日,用 8 位数字 20100102 表示,它是回文的。
对于 2010 年 10 月 2 日,用 8 位数字 20101002 表示,它不是回文的。
输入格式
输入包括两行,每行包括一个 8 位数字。

第一行表示牛牛指定的起始日期 date1,第二行表示牛牛指定的终止日期 date2。保证 date1 和 date2 都是真实存在的日期,且年份部分一定为 4 位数字,且首位数字不为 0。

保证 date1 一定不晚于 date2。

输出格式
输出共一行,包含一个整数,表示在 date1 和 date2 之间,有多少个日期是回文的。

输入样例:

20110101
20111231

输出样例:

1

思路分析

在这里插入图片描述

在这里插入图片描述

题解

#include<iostream>
#include<cstring>

using namespace std;

//存月的天数
int days[13] = {0 , 31 , 28 , 31, 30 , 31 , 30 , 31 , 31, 30 , 31, 30 , 31};

//判断日期是否合法
bool check_valid(int date){
    int year = date / 10000;
    int month = date % 10000 / 100;
    int day = date % 100;
    
    if(month == 0 || month > 12){
        return false;
    }
    
    if (day == 0 || month != 2 && day > days[month]){
        return false;
    }
    
    //判断平年闰年
    if(month == 2){
        int leap = year % 100 && year % 4 == 0 || year % 400 == 0;
        if(day > 28 + leap){
            return false;
        }
    }
}


int main(){
    
    //读入
    int date1 , date2;
    cin >> date1 >> date2 ; 
    
    int res =0 ;
    for(int i = 1000 ; i < 10000 ; i++){
        int date = i , x = i;
        //翻过来接上
        for(int j = 0 ; j < 4 ; j++){
            date = date * 10 + x % 10;
            x /= 10;
        }
        
        if(date1 <= date && date <= date2 && check_valid(date)){
            res ++;
        }
        
    }
    cout << res << endl;
    return 0;
}

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
C++中设计一个回文日期管理系统,主要是为了创建一个能识别和处理那些在正常读写顺序下前后完全相同的日期格式的程序。这种系统通常会关注年月日的组合,例如"02/02/2022"或"12/12/2012"这样的日期,因为它们是回文。 以下是一个简单的概念框架: 1. 定义日期结构:首先,你需要创建一个日期类(Date)来存储年、月、日的数据,可能包括私有变量如`year`, `month`, 和 `day`,以及公共方法如`setDate()`, `isPalindrome()`等。 ```cpp class Date { private: int year, month, day; public: void setDate(int y, int m, int d); bool isPalindrome(); }; ``` 2. 实现日期设置和检查:`setDate()`函数用来设置日期,而`isPalindrome()`函数则检查输入的日期是否为回文。 ```cpp void Date::setDate(int y, int m, int d) { year = y; month = m; day = d; } bool Date::isPalindrome() { std::string formattedDate = std::to_string(day) + "/" + std::to_string(month) + "/" + std::to_string(year); return formattedDate == std::string(formattedDate.rbegin(), formattedDate.rend()); } ``` 3. 用户接口:创建一个控制台应用或者类,提供与用户的交互,让用户输入日期并检查是否是回文。 ```cpp int main() { Date today; int inputYear, inputMonth, inputDay; // 获取用户输入并设置日期 std::cout << "Enter a date (format: DD/MM/YYYY): "; std::cin >> inputDay >> inputMonth >> inputYear; today.setDate(inputYear, inputMonth, inputDay); // 检查并输出结果 if (today.isPalindrome()) { std::cout << "The date is a palindrome.\n"; } else { std::cout << "The date is not a palindrome.\n"; } return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ding Jiaxiong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值