蓝桥杯:回文日期问题

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

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

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

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

输入描述

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

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

输出描述

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

输入输出样例

示例

输入

20200202

输出

20211202
21211212

运行限制

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

 

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
     
                String date = scan.next();
        char[] dates = date.toCharArray();
        boolean d1 = false, d2 = false;
        int year = 0, month = 0, day = 0;
        Integer s = 0;
        while(true) {

                s = Integer.valueOf(dates[dates.length - 2] + "" + dates[dates.length - 1]);

            s ++;
            dates[dates.length - 2] =  (char)((s / 10) + 48);
            dates[dates.length - 1] =  (char)((s % 10) + 48);
           year = Integer.valueOf(dates[0] + "" + dates[1] + dates[2] + dates[3] );
           month = (dates[4] - 48 ) * 10 + (dates[5] - 48);
           day = s;
        /*   try{
           }catch (Exception e){
               System.out.println(day);

           }*/


           //1 3 5 7 8 10 12
            // 2  4 6 9 11

            switch (month){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    if(day > 32) {
                        dates[dates.length - 1] = '1';
                        dates[dates.length - 2] = '0';
//                        date = date /
                        month ++;
                        if(month > 12){
                            year ++;
                            dates[0] = (char) (year / 1000 + 48);
                            dates[1] = (char) (year / 100 % 10 + 48);
                            dates[2] = (char) (year / 10 % 10 + 48);
                            dates[3] = (char) (year % 10  + 48);

                            dates[4] = '0';
                            dates[5] = '1';

                        } else {
                            dates[4] = (char)(month / 10 + 48);
                            dates[5] = (char)(month % 10 + 48);
                        }
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    if(day > 31) {
                        dates[dates.length - 1] = '1';
                        dates[dates.length - 2] = '0';
//                        date = date /
                        month ++;

                        dates[4] = (char)(month / 10 + 48);
                        dates[5] = (char)(month % 10 + 48);
            
                    }
                     break;
                case 2:
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {

                        if(day > 29){
                            dates[6] = '0';
                            dates[7] = '0';


                            dates[4] = '0';
                            dates[5] = '3';

                        }
                       /* else {
                            dates[6] = (char)(day / 10 + 48);
                            dates[7] = (char)(day % 10 + 48);
                        }*/
                    } else {
                        if(day > 28){
                            dates[6] = '0';
                            dates[7] = '0';

                            dates[4] = '0';
                            dates[5] = '3';
                        } /*else {
                            dates[6] = (char)(day / 10 + 48);
                            dates[7] = (char)(day % 10 + 48);
                        }*/
                    }
            }
            //判断 ABBC CBBA 回文数
            if(d1 == false && dates[3] == dates[4]){
                String str1 = dates[0] + "" + dates[1] + dates[2];
                String str2 = dates[7] + "" + dates[6] + dates[5];
                if(str1.equals(str2)){
                    str1 = "";
                    d1 = true;
                    for(int i = 0; i < dates.length; i++){
                        str1  += "" + dates[i];
                    }
                    System.out.println(str1);
                }
            }
            //判断ABAB BABA
            if(d2 == false && dates[3] == dates[4] && dates[3] == dates[1] && dates[4] == dates[6] &&
                    dates[0] == dates[2] && dates[dates.length - 1] == dates[dates.length - 3]
                    && dates[0] != dates[1] && dates[dates.length - 1] != dates[dates.length - 2]){
                String str1 = dates[0] + "" + dates[1] + dates[2];
                String str2 = dates[5] + "" + dates[6] + dates[7];
                if(str1.equals(str2)){
                    str1 = "";
                    d2 = true;
                    for(int i = 0; i < dates.length; i++){
                        str1  += "" + dates[i];
                    }
                    System.out.println(str1);
                }
            }
            if(d1 == true && d2 == true){
                break;
            }

        }
        scan.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值