java算法(回文日期)

题目描述:

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

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

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

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

输入描述:

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

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

输出描述:

  • 输出是否是合法日期

  • 输出回文日期和 ABABBABA 型的回文日期

解题思路:

一.确定回文数:

1.日期格式:yyyymdmd

2.确定abcd efgh日期 ——取出每一个数字

3.然后确定相等(利用 a==h&&b==g&&c==f&&d==e),最后print

二.ab形的回文日期:

1.ababbaba,在上面的基础上,再加一个 a==c&&a==e&&a==g 以及 b==d&&b==f&&b==h

确保 aceg都相等 bdfh都相等。

需要注意的事情为:大小月,平闰年。(这个地方利用Calendar类 localdate)以及日期的限制条件等。

思路:

一.先判断用户输入的日期是否是合法的

创建一个静态的方法isLegaDate()

private static boolean isLegalDate(String strDate) {
        int year, month, day;
        //这里也是提取年月日
        year = Integer.parseInt(strDate.substring(0, 4));
        month = Integer.parseInt(strDate.substring(4, 6));
        day = Integer.parseInt(strDate.substring(6));

        if (year < 1000 || year > 8999)
            return false;
        if (month < 1 || month > 12)
            return false;

        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            if (day < 1 || day > 31)
                return false;
            //2月
        } else if (month == 2) {
            //闰年
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                if (day < 1 || day > 29)
                    return false;
            } else {
                if (day < 1 || day > 28)
                    return false;
            }
        } else {
            if (day < 1 || day > 30)
                return false;
        }
        return true;
    }

二.判断输入的日期是不是回文日期

定义一个静态的方法isPalindromicDate()方法

private static boolean isPalindromiDate(String strDate) {
        for (int i = 0; i < 4; i++) {
//判断前后对应的字母是否一样
            if (strDate.charAt(i) != strDate.charAt(7 - i))
                return false;
        }
        return true;
    }

三.判断输入的日期是否是ABABBABA型回文日期

定义一个静态方法isABABBABAPalindromicDate()

private static boolean isABABBABADate(String strDate) {
        if (isPalindromiDate(strDate)) {
            //判断对称的字母是否相同
            if (strDate.charAt(0) == strDate.charAt(2) && strDate.charAt(1) == strDate.charAt(3))
                return true;
        }
        return false;
    }

四.主函数

  1. 首先测试输入的日期是否是合法日期,定义int类型的年月日,再用if语句进行判断,if (isLegalDate(strDate))则返回true,反之返回false。

  1. 接下来如果是合法日期,则要判断是否是回文日期并输出。

  • 用Integer.parseInt()方法将整型数据Integer转换为基本数据类型int

  • 再用substring()方法截取年月日

  • 再用calendar.set()将截取到的年月日设置为起点

  • 最后用if语句判断是否是回文函数,调用isPalindromiDate()方法,如果是回文日期则输出该回文日期

  1. 最后判断是否是ABABBABA型回文日期并输出

  • 和回文日期程序操作类似,调用isABABBABADate()方法,如果是ABABBABA型回文日期则进行输出。

public static void main(String[] args) {
        String strDate = "";
        //年月日
        int year, month, day;
        Scanner sc = new Scanner(System.in);
        Calendar calendar = Calendar.getInstance();//创建日历类对象
        //SimpleDateFormat类是DateFormat的子类,叫日期格式化类,专门用来格式化和解析日期的
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        System.out.print("输入8位数构成的日期:");
        strDate = sc.next();

        // 判断日期是否合法
        if (isLegalDate(strDate)) {
            System.out.println("[" + strDate + "]是合法日期~");
            // 用户输入的日期作为起点
            //Integer.parseInt是将整型数据Integer转换为基本数据类型int
            //strDate.substring(0, 4)是从下标为0的位置开始截取到下标为4的位置(不包括下标为4的值),即截取年份
            year = Integer.parseInt(strDate.substring(0, 4));
            month = Integer.parseInt(strDate.substring(4, 6));//截取月份
            day = Integer.parseInt(strDate.substring(6));//从下标位6的位置截取到最后
            //用calendar.set()设置年月日
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month - 1);
            calendar.set(Calendar.DAY_OF_MONTH, day);
            // 输出此日期之后的第一个回文日期
            String strDate1 = "";
            Calendar calendar1 = calendar;
            while (true) {
                calendar1.add(Calendar.DAY_OF_MONTH, 1);//nowDate+1天,一天后的日期
                strDate1 = sdf.format(calendar.getTime());//getTime()方法用于通过使用Date实例获取此calendar1的时间值。
                if (isPalindromiDate(strDate1)) {
                    break;
                }
            }
            System.out.println("此日期之后的第一个回文日期:" + strDate1);

            // 输出该日期之后的第一个ABABBABA型回文日期
            String strDate2 = "";
            Calendar calendar2 = calendar;
            while (true) {
                calendar2.add(Calendar.DAY_OF_MONTH, 1);// 后推1天
                strDate2 = sdf.format(calendar2.getTime());
                if (isABABBABADate(strDate2)) {
                    break;
                }
            }
            System.out.println("此日期之后的第一个ABABBABA回文日期:" + strDate2);
        } else {
            System.out.println("[" + strDate + "]是非法日期~");
        }
    }

完整代码:

package Lq_算法练习;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class Demo_回文日期01 {
    public static void main(String[] args) {
        String strDate = "";
        //年月日
        int year, month, day;
        Scanner sc = new Scanner(System.in);
        Calendar calendar = Calendar.getInstance();//创建日历类对象
        //SimpleDateFormat类是DateFormat的子类,叫日期格式化类,专门用来格式化和解析日期的
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        System.out.print("输入8位数构成的日期:");
        strDate = sc.next();

        // 判断日期是否合法
        if (isLegalDate(strDate)) {
            System.out.println("[" + strDate + "]是合法日期~");
            // 用户输入的日期作为起点
            //Integer.parseInt是将整型数据Integer转换为基本数据类型int
            //strDate.substring(0, 4)是从下标为0的位置开始截取到下标为4的位置(不包括下标为4的值),即截取年份
            year = Integer.parseInt(strDate.substring(0, 4));
            month = Integer.parseInt(strDate.substring(4, 6));//截取月份
            day = Integer.parseInt(strDate.substring(6));//从下标位6的位置截取到最后
            //用calendar.set()设置年月日
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month - 1);
            calendar.set(Calendar.DAY_OF_MONTH, day);
            // 输出此日期之后的第一个回文日期
            String strDate1 = "";
            Calendar calendar1 = calendar;
            while (true) {
                calendar1.add(Calendar.DAY_OF_MONTH, 1);//nowDate+1天,一天后的日期
                strDate1 = sdf.format(calendar.getTime());//getTime()方法用于通过使用Date实例获取此calendar1的时间值。
                if (isPalindromiDate(strDate1)) {
                    break;
                }
            }
            System.out.println("此日期之后的第一个回文日期:" + strDate1);

            // 输出该日期之后的第一个ABABBABA型回文日期
            String strDate2 = "";
            Calendar calendar2 = calendar;
            while (true) {
                calendar2.add(Calendar.DAY_OF_MONTH, 1);// 后推1天
                strDate2 = sdf.format(calendar2.getTime());
                if (isABABBABADate(strDate2)) {
                    break;
                }
            }
            System.out.println("此日期之后的第一个ABABBABA回文日期:" + strDate2);
        } else {
            System.out.println("[" + strDate + "]是非法日期~");
        }
    }

    /*
     判断输入的日期是否合法
    */
    private static boolean isLegalDate(String strDate) {
        int year, month, day;
        //这里也是提取年月日
        year = Integer.parseInt(strDate.substring(0, 4));
        month = Integer.parseInt(strDate.substring(4, 6));
        day = Integer.parseInt(strDate.substring(6));

        if (year < 1000 || year > 8999)
            return false;
        if (month < 1 || month > 12)
            return false;

        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            if (day < 1 || day > 31)
                return false;
        } else if (month == 2) {//2月
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {//闰年
                if (day < 1 || day > 29)
                    return false;
            } else {
                if (day < 1 || day > 28)
                    return false;
            }
        } else {
            if (day < 1 || day > 30)
                return false;
        }
        return true;
    }

    /*
     判断是否是回文日期
    */
    private static boolean isPalindromiDate(String strDate) {
        for (int i = 0; i < 4; i++) {
            if (strDate.charAt(i) != strDate.charAt(7 - i))
                return false;
        }
        return true;
    }

    /*
     判断是否是ABABBABA型回文日期
    */
    private static boolean isABABBABADate(String strDate) {
        if (isPalindromiDate(strDate)) {
            //判断对称的字母是否相同
            if (strDate.charAt(0) == strDate.charAt(2) && strDate.charAt(1) == strDate.charAt(3))
                return true;
        }
        return false;
    }
}

运行结果示例1:

运行结果示例2:

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值