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

    你可能感兴趣的文章
    python3+requests+unittest实战系列【二】
    查看>>
    Python3+Requests+Unittest实战系列【二】
    查看>>
    python3----ASCII
    查看>>
    Python3-selenium自动化测试框架,结合HTMLTestRunner测试报告!
    查看>>
    Python3-如何自我更新作为服务运行的应用程序
    查看>>
    Python3.1-网格模拟概念性问题
    查看>>
    Python3.12 解决conda环境libstdc++版本过低问题
    查看>>
    python3.2批量查询IP地址区域
    查看>>
    Python3.5中协程和未来/任务之间的区别?
    查看>>
    python3.5实现socket通讯(TCP)
    查看>>
    python3.6 ubuntu部署nginx、 uwsgi、 django
    查看>>
    Python3.6.3中,functools似乎不能用
    查看>>
    python3.6.5+cuda9+cudnn7.1+win10+tensorflow-gpu1.9.0下载配置
    查看>>
    python3.6使用pickle序列化class
    查看>>
    python3.6入门到高阶(全栈) day027 网络通信协议 osi七层模型
    查看>>
    Python3.6属性错误:模块‘;异步没有属性‘;运行‘;
    查看>>
    python3.6版本中的python-weka-wrapper安装失败,错误代码为%1
    查看>>
    Python3.7安装cv2踩过的坑
    查看>>
    python写txt文件
    查看>>
    python关于sorted排序函数详细解析 附实战代码(全)
    查看>>