博客
关于我
回文日期
阅读量: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/

    你可能感兴趣的文章
    Postgres invalid command \N数据恢复处理
    查看>>
    Postgres like 模糊查询匹配集合
    查看>>
    Postgres 自定义函数内实现 in 操作符的递归查询
    查看>>
    Postgres 返回当前时间前后指定天数的集合
    查看>>
    postgres--vacuum
    查看>>
    postgres--wal
    查看>>
    postgres--流复制
    查看>>
    postgres10配置huge_pages
    查看>>
    PostgreSQL 10.0 preview 变化 - pg_xlog,pg_clog,pg_log目录更名为pg_wal,pg_xact,log
    查看>>
    PostgreSQL 10.1 手册_部分 II. SQL 语言_第 15章 并行查询_15.2. 何时会用到并行查询?...
    查看>>
    PostgreSQL 10.1 手册_部分 II. SQL 语言_第 9 章 函数和操作符_9.23. 行和数组比较
    查看>>
    PostgreSQL 10.1 手册_部分 III. 服务器管理_第 21 章 数据库角色
    查看>>
    Postgresql 12.9如何配置允许远程连接
    查看>>
    PostgreSQL 9.6 同步多副本 与 remote_apply事务同步级别 应用场景分析
    查看>>
    Postgresql CopyManager 流式批量数据入库
    查看>>
    PostgreSQL cube 插件 - 多维空间对象
    查看>>
    PostgreSQL Daily Maintenance - cluster table
    查看>>
    PostgreSQL on Linux 最佳部署手册
    查看>>
    PostgreSQL Oracle 兼容性之 - pipelined
    查看>>
    PostgreSQL Point-In-Time Recovery (Incremental Backup)
    查看>>