• 蓝桥-回文日期


    题目

    题目描述

    2020 年春节期间,有一个特殊的日期引起了大家的注意:2020 年 2 月 2 日。因为如果将这个日期按 “yyyymmdd” 的格式写成一个 8 位数是 20200202,恰好是一个回文数。我们称这样的日期是回文日期。

    有人表示 20200202 是 “千年一遇” 的特殊日子。对此小明很不认同,因为不到 2 年之后就是下一个回文日期:20211202 即 2021 年 12 月 2 日。

    也有人表示 20200202 并不仅仅是一个回文日期,还是一个 ABABBABA 型的回文日期。对此小明也不认同,因为大约 100 年后就能遇到下一个 ABABBABA 型的回文日期:21211212 即 2121 年 12 月 12 日。算不上 “千年一遇”,顶多算 “千年两遇”。

    给定一个 8 位数的日期,请你计算该日期之后下一个回文日期和下一个 ABABBABA 型的回文日期各是哪一天。

    输入描述

    输入包含一个八位整数 NNN,表示日期。

    对于所有评测用例,10000101≤N≤8999123110000101 \leq N \leq 8999123110000101N89991231,保证 NNN 是一个合法日期的 8 位数表示。

    输出描述

    输出两行,每行 1 个八位数。第一行表示下一个回文日期,第二行表示下一个 ABABBABA 型的回文日期。

    输入输出样例

    示例

    输入

    20200202
    
    • 1
    • 2

    输出

    20211202
    21211212
    • 1
    • 2

    运行限制

    • 最大运行时间:1s
    • 最大运行内存: 256M

    题解

    这里需要的是月的范围是[1,12],每月天数的范围要计算,计算这里要记住的是2月闰年的时是29天,其它时候是28天,其它月份小学的时候教过怎么数手指,握着手,数背面的突起和凹下的,从左到右边数。1月大,二月小,三月大,四月小,五月大,六月小、七月大、八月大、九月小、十月大、十一月小、十二月大。除了二月,其他的大的是31天,小的是30天。

    import java.util.*;
    
    public class Main {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int N = 8;
            int[] nums = new int[N];
            //在此输入您的代码...
            while (scan.hasNext()) {
                int a = 0, b = 0;
                int code = scan.nextInt();
                for (int i = code + 1; i < 1e9; i++) {
                    int m = i, c = 0;
                    while (c < N) {
                        nums[c++] = m % 10;
                        m /= 10;
                    }
                    int year = nums[0] * 1000 + nums[1] * 100 + nums[2] * 10 + nums[3];
                    int mouth = nums[4] * 10 + nums[5];
                    int day = nums[6] * 10 + nums[7];
                    if ((mouth == 0 || mouth > 12) || (day == 0 ||  day > getDay(year, mouth))) continue;
                    if (verify(nums)) {
                        if (a == 0) a = i;
                        if (lr(nums)) {
                            b = i;
                            break;
                        }
                    }
                }
                System.out.println(a + "\n" + b);
            }
            scan.close();
    
        }
    
        private static boolean verify(int[] array) {
            int l = 0, r = array.length - 1;
            while (l <= r) {
                if (array[l++] != array[r--]) return false;
            }
            return true;
        }
    
        private static boolean lr(int[] array) {
            int l = 0;
            int a = array[0], b = array[1];
            if (a == b) return false;
            while (l < 4) {
                if (array[l] != a) return false;
                if (array[l + 1] != b) return false;
                l += 2;
            }
            while (l < 8) {
                if (array[l] != b) return false;
                if (array[l + 1] != a) return false;
                l += 2;
            }
            return true;
        }
    
        private static int getDay (int year, int mouth) {
            switch (mouth) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
            }
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) return 29;
            return 28;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
  • 相关阅读:
    【linux内核中的双向链表-02】list_for_each_safe
    中小型评估机构信息化方案研究
    正态分布的推导笔记
    去哪儿网2023正式秋招啦,来这里可以内推
    Python面向对象2-继承-
    水处理行业污水处理厂电能质量监测与治理系统解决方案-安科瑞黄安南
    HDLBits: 在线学习 SystemVerilog(七)-Problem 28-31
    D. Count GCD(数论/gcd/素数筛/容斥)
    孤独、自卑、不合群?80%可能是社交恐惧症
    使用Golang调用摄像头
  • 原文地址:https://blog.csdn.net/qq_45716444/article/details/127992858