栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

项目二

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

项目二

文章目录
    • 任务1:蓝桥杯竞赛题:回文日期问题
    • 任务2:数组排序(升序和降序排列)
      • 1、选择法排序
      • 2、冒泡法排序


任务1:蓝桥杯竞赛题:回文日期问题
  • 2020 年春节期间,有一个特殊的日期引起了大家的注意:2020 年 2 月 2 日。因为如果将这个日期按“yyyymmdd”的格式写成一个 8 位数是 20200202,恰好是一个回文数。我们称这样的日期是回文日期。有人表示 20200202 是“千年一遇”的特殊日子。对此小明很不认同,因为不到 2 年之后就是下一个回文日期:20211202 即 2021年 12月2日。也有人表示 20200202 并不仅仅是一个回文日期,还是一个 ABABBABA 型的回文日期。对此小明也不认同,因为大约 100 年后就能遇到下一个 ABABBABA 型的回文日期:21211212即2121 年 12 月 12 日。算不上“千年一遇”,顶多算“千年两遇”。

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

  • 输入描述:输入包含一个八位整数 N,表示日期。
    对于所有评测用例,10000101 ≤ N ≤ 89991231,保证 N 是一个合法日期的 8 位数。

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

  • 源代码

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


public class PalindromicDate {
    
    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) {
            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;
    }
    public static void main(String[] args) {
        String strDate;
        int year,month,day;
        Scanner sc = new Scanner(System.in);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

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

        if (isLegalDate(strDate)) {
            System.out.println("【" + strDate + "】是合法日期。");
            //获取用户输入的合法日期作为循环起点
            year = Integer.parseInt(strDate.substring(0,4));
            month = Integer.parseInt(strDate.substring(4,6));
            day = Integer.parseInt(strDate.substring(6));
            Calendar calendar = Calendar.getInstance();
            calendar.set(year,month-1,day);
            //任务1:输出该日期之后的第一个回文日期
            String strDate1 = "";
            Calendar calendar1 = calendar;
            while (true) {
                calendar1.add(Calendar.DAY_OF_MONTH, 1);//往后推一天
                strDate1 = sdf.format(calendar1.getTime());
                if (isPalindromicDate(strDate1)) {
                    break;
                }
            }
            System.out.println("该日期之后的第一个回文日期:" + strDate1);
            //任务2:输出该日期之后的第一个ABABBABA型回文日期
            String strDate2 = "";
            Calendar calendar2 = calendar;
            while (true) {
                calendar2.add(Calendar.DAY_OF_MONTH, 1);
                strDate2=sdf.format(calendar2.getTime());
                if (isABABBABAPalindromicDate(strDate2)) {
                    break;
                }
            }
            System.out.println("此日期之后的第一个ABABBABA型回文日期:" + strDate2);
        } else {
            System.out.println("【" + strDate + "】是非法日期。");
        }
    }
    private static boolean isPalindromicDate(String strDate) {
        for (int i=0;i<4;i++) {
            if (strDate.charAt(i) != strDate.charAt(7-i)) return false;
        }
        return true;
    }
    private static boolean isABABBABAPalindromicDate(String strDate) {
        if (isPalindromicDate(strDate)) {
            if (strDate.charAt(0) == strDate.charAt(2) && strDate.charAt(1) == strDate.charAt(3)) {
                return true;
            }
        }
        return false;
    }
}
  • 运行结果

任务2:数组排序(升序和降序排列) 1、选择法排序
  • 定义

选择排序(Selection Sort)是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法。

  • 源代码
public class XEm02 {
    //选择升序排序方法
    public static void choiceSort(Integer[] a) {
        if (a == null || a.length <= 0) {
            return;
        }
        for (int i = 0; i < a.length; i++) {
            int min = i;
            //将当前下标定义为最小值下标
            for (int j = i + 1; j < a.length; j++) {
                if (a[min] > a[j]) {
                    //如果有小于当前最小值的关键字
                    min = j;
                    //将此关键字的下标赋值给min
                }
            }
            if (i != min) {
                //若min不等于i,说明找到最小值,交换
                int tmp = a[min];
                a[min] = a[i];
                a[i] = tmp;
            }
        }
    }
    //主程序
    public static void main(String[] args) {
        Integer[] arr = {71,54,58,29,31,78,2,77,82};

        System.out.print("排序前:");
        for (int x : arr) {
            System.out.print(x + "t");
        }

        System.out.println();

        System.out.print("排序后:");
        //调用方法
        choiceSort(arr);
        for (int x : arr) {
            System.out.print(x + "t");
        }
    }
}
  • 运行结果

2、冒泡法排序
  • 定义

冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端。

  • 源代码
import java.lang.reflect.Array;
import java.util.Arrays;


public class XEm01 {
    //冒泡排序方法
    public static void sort(int arr[]) {
        //i表示第几轮“冒泡”,j 表示“走访”到的元素索引。
        // 每一轮“冒泡”中,j 需要从列表开头“走访”到 arr.length - 1 的位置。
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
    //主程序
    public static void main(String[] args) {
        int[] arr = new int[] {71,54,58,29,31,78,2,77,82};
        System.out.print("排序前:");
        for (int x : arr) {
            System.out.print(x + "t");
        }
        
        System.out.println();

        System.out.print("排序后:");
        XEm01.sort(arr);
        for (int x : arr) {
            System.out.print(x + "t");
        }
    }
}
  • 运行结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/922333.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号