顺子日期 蓝桥杯

调用API

思路: 设置Calendar的属性,获取Calendar的毫秒数,转换成指定格式的字符串(yyyyMMdd),判断字符串中是否包含符合条件的,若有就+1,
迭代: 每次循环给Calendar加上一天即可

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
   
    public static void main(String[] args) {
   
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2022);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        //"234","345","456","567","678","789"
        // 上面的这么多不可能出现
        String[] strs = {
   "012","123"};
        int cnt = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        while (calendar.get(Calendar.YEAR) == 2022) {
   
            String s = sdf.format(calendar.getTimeInMillis());
            for(String str: strs){
   
                if(s.contains(str))
                {
   
                    cnt++;
                    break;
                }
            }
            calendar.add(Calendar.DAY_OF_MONTH,1);
        }
        System.out.println(cnt);
    }
}

模拟

前提是要知道闰年的条件

  1. 能被400整除
  2. 是4的倍数而不是100的倍数
    满足上面条件的其中一个即可
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
   
    //1 3 5 7 8 10 12
    //2 4 6 9 11
    static int[] days = {
   
            0,
            31,
            28,
            31,
            30,
            31,
            30,
            31,
            31,
            30,
            31,
            30,
            31,0
    };
    static int day = 1, month =1 ,year = 2022;

    public static boolean check(){
   
        int num = year * 100 + month+day;
        String s = num+"";
        // 2022 10 11
        int steps = 1;
        for(int i=0;i<s.length()-1;i++) {
   
            int pre = s.charAt(i);
            int next = s.charAt(i+1);
            if(next == pre+1){
   
                steps ++;
            }else {
   
                steps = 1;
            }
            if(steps == 3)
                return true;
        }
        return false;
    }
    public static void main(String[] args) {
   
       int ans = 0;
       while(year==2022){
   

           if(check()){
   
               ans++;
           }
           day++;
           if(month==2){
   
               if(year%400==0 || year%4==0 && year%100!=0){
   
                   if(day>29)
                   {
   
                       day=1;
                       month++;
                   }
               }else
               {
   
                   if(day>28){
   
                       day=1;
                       month++;
                   }
               }
           }else if(day>days[month]){
   //不加的话这里会溢出
                day=1;
                month++; // 当day =32,month = 12的时候,month会加1=>13,走到day>days[month]的时候会下标越界
           }
            if(month>12)
                year++;
       }
        System.out.println(ans);
    }
}

相关推荐

  1. 日期

    2024-02-19 00:32:03       48 阅读
  2. 】(完全日期

    2024-02-19 00:32:03       35 阅读
  3. 完全日期

    2024-02-19 00:32:03       34 阅读
  4. -矩阵

    2024-02-19 00:32:03       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-19 00:32:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-19 00:32:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-19 00:32:03       87 阅读
  4. Python语言-面向对象

    2024-02-19 00:32:03       96 阅读

热门阅读

  1. 【orbslam2+nerf】

    2024-02-19 00:32:03       52 阅读
  2. Python 键盘模拟

    2024-02-19 00:32:03       52 阅读
  3. 24 双非计算机秋招总结

    2024-02-19 00:32:03       50 阅读
  4. 数据库事务的 4 种隔离级别

    2024-02-19 00:32:03       47 阅读
  5. C Primer Plus(第六版)16.17 复习题 第6题

    2024-02-19 00:32:03       50 阅读
  6. 110 C++ decltype含义,decltype 主要用途

    2024-02-19 00:32:03       41 阅读
  7. python - 文件

    2024-02-19 00:32:03       59 阅读
  8. C++练习

    C++练习

    2024-02-19 00:32:03      48 阅读
  9. 算法训练营day28(补), 贪心算法2

    2024-02-19 00:32:03       54 阅读
  10. Hive使用双重GroupBy解决数据倾斜问题

    2024-02-19 00:32:03       58 阅读
  11. Qt - 不同类之间槽函数和信号的连接

    2024-02-19 00:32:03       46 阅读