博客
关于我
回文日期
阅读量:378 次
发布时间:2019-03-05

本文共 3990 字,大约阅读时间需要 13 分钟。

为了解决这个问题,我们需要找出在指定的两个日期之间(包含这两个日期本身),有多少个日期是回文的。牛牛定义一个日期是回文的,当且仅当表示这个日期的8位数字是回文的。

方法思路

  • 问题分析

    • 牛牛用8位数字表示日期,前四位是年份,中间两位是月份,最后两位是日期。
    • 一个日期是回文的,当且仅当这个8位数字本身是回文的,也就是说,从左到右第i个数字和从右到左第i个数字相同。
  • 关键步骤

    • 将输入的两个日期转换为年、月、日的数值形式。
    • 遍历从起始日期到终止日期之间的所有日期,生成对应的8位数字字符串。
    • 检查每个字符串是否是回文,并且是否有效日期。
  • 有效日期检查

    • 月份必须在1到12之间。
    • 天数必须符合该月份的天数,包括闰年的处理。
  • 回文检查

    • 将字符串倒序检查每个字符是否相同。
  • 解决代码

    #include 
    #include
    using namespace std;int max_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};bool is_leap(int year) { if (year % 4 != 0) return false; if (year % 100 == 0) return (year % 400 == 0); return true;}bool is_valid_date(int year, int month, int day) { if (month < 1 || month > 12) return false; if (day < 1) return false; if (month == 2) { if (!is_leap(year)) { return day <= 28; } else { return day <= 29; } } else if (month == 4 || month == 6 || month == 9 || month == 11) { return day <= 30; } return day <= max_days[month - 1];}int main() { string s1, s2; cin >> s1 >> s2; int year1 = s1[0] * 1000 + s1[1] * 100 + s1[2] * 10 + s1[3]; int month1 = s1[4] * 10 + s1[5]; int day1 = s1[6] * 10 + s1[7]; int year2 = s2[0] * 1000 + s2[1] * 100 + s2[2] * 10 + s2[3]; int month2 = s2[4] * 10 + s2[5]; int day2 = s2[6] * 10 + s2[7]; int current_year = year1; int current_month = month1; int current_day = day1; int target_year = year2; int target_month = month2; int target_day = day2; int count = 0; while (true) { string current_str = to_string(current_year) + to_string(current_month).zfill(2) + to_string(current_day).zfill(2); bool is_palindrome = true; for (int i = 0; i < 8; ++i) { if (current_str[i] != current_str[7 - i]) { is_palindrome = false; break; } } if (is_palindrome) { if (is_valid_date(current_year, current_month, current_day)) { count++; } } current_day++; if (current_day > max_days[current_month - 1]) { current_month++; current_day = 1; if (current_month > 12) { current_year++; current_month = 1; } if (current_month == 2) { if (is_leap(current_year)) { if (current_day > 29) { current_month++; current_day = 1; if (current_month > 12) { current_year++; current_month = 1; } } } else { if (current_day > 28) { current_month++; current_day = 1; if (current_month > 12) { current_year++; current_month = 1; } } } } else if (current_month == 4 || current_month == 6 || current_month == 9 || current_month == 11) { if (current_day > 30) { current_month++; current_day = 1; if (current_month > 12) { current_year++; current_month = 1; } } } if (current_year > target_year) { break; } } if (current_year > target_year || (current_year == target_year && (current_month > target_month || (current_month == target_month && current_day > target_day)))) { break; } } cout << count << endl; return 0;}

    代码解释

  • 读取输入:从标准输入读取两个日期字符串。
  • 分解日期:将每个日期字符串分解为年、月、日的数值。
  • 遍历日期:从起始日期开始,逐个递增一天,生成对应的日期字符串。
  • 回文检查:检查每个日期字符串是否是回文。
  • 有效日期检查:确保生成的日期是有效的。
  • 计数满足条件的日期:如果日期是回文且有效,计数加一。
  • 终止条件:当当前日期超过终止日期时,终止循环并输出结果。
  • 转载地址:http://bubwz.baihongyu.com/

    你可能感兴趣的文章
    node中的get请求和post请求的不同操作【node学习第五篇】
    查看>>
    Node中的Http模块和Url模块的使用
    查看>>
    Node中自启动工具supervisor的使用
    查看>>
    Node入门之创建第一个HelloNode
    查看>>
    node全局对象 文件系统
    查看>>
    Node出错导致运行崩溃的解决方案
    查看>>
    Node响应中文时解决乱码问题
    查看>>
    node基础(二)_模块以及处理乱码问题
    查看>>
    node安装卸载linux,Linux运维知识之linux 卸载安装node npm
    查看>>
    node安装及配置之windows版
    查看>>
    Node实现小爬虫
    查看>>
    Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
    查看>>
    Node提示:npm does not support Node.js v12.16.3
    查看>>
    Node搭建静态资源服务器时后缀名与响应头映射关系的Json文件
    查看>>
    Node服务在断开SSH后停止运行解决方案(创建守护进程)
    查看>>
    node模块化
    查看>>
    node模块的本质
    查看>>
    node环境下使用import引入外部文件出错
    查看>>
    node环境:Error listen EADDRINUSE :::3000
    查看>>
    Node的Web应用框架Express的简介与搭建HelloWorld
    查看>>