当前位置: 首页 > news >正文

蓝桥杯杯赛-日期模拟

知识点

处理日期

1. 按天枚举日期:逐天遍历起始日期到结束日期范围内的每个日期。

2. 处理闰年:正确判断闰年条件。闰年定义为:年份 满足以下任意一个条件:(闰年的2月只有29天)

满足下面一个条件就是闰年

1> 是 400 的倍数。

2> 是 4的倍数但不是 100的倍数

艺术与篮球

已知当前是星期几求出某个月的某一天是星期几

int week=x;x为第一天的日期

week = week%7+1求出每个星期的下一天

题目链接

1.艺术与篮球 - 蓝桥云课

题目解析

判断笔画数是不是超过50,超过50就去练习书法

算法原理

使用哈希表,把笔画和对应的数字联系在一起

使用日期函数来标记时间的开始和结尾,然后遍历每一天去计算笔画数

代码编写

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<Character, Integer> map = new HashMap<Character, Integer>() {{
            put('0', 13);  // 零
            put('1', 1);   // 一
            put('2', 2);   // 二
            put('3', 3);   // 三
            put('4', 5);   // 四
            put('5', 4);   // 五
            put('6', 4);   // 六
            put('7', 2);   // 七
            put('8', 2);   // 八
            put('9', 2);   // 九
        }};

        LocalDate startDate = LocalDate.of(2000, 1, 1);
        LocalDate endDate = LocalDate.of(2024, 4, 14);
        int days = 0;

        while (startDate.isBefore(endDate)) {
            String s = startDate.toString().replace("-", "");
            int total = 0;

            // 通过map查询笔画数
            for (char c : s.toCharArray()) {
                total += map.get(c);
            }

            if (total > 50) {
               days++;
            }
            startDate = startDate.plusDays(1);
        }

        System.out.println(days);
    }
}

从哪开始从哪结束

LocalDate startDate = LocalDate.of(2000, 1, 1);

LocalDate endDate = LocalDate.of(2024, 4, 14);

while 循环会一直执行,直到 startDate 等于或超过 endDate。循环会逐日增加 startDate,并计算当天日期的数字笔画数。

startDate.isBefore(endDate)

转为Srting类型,并且去除-

 startDate.toString().replace("-", "");

自增天数

  startDate = startDate.plusDays(1);

补充: 获得xxx年xxx月xx日是星期几

可以使用

LocalDate date = LocalDate.of(2026, 4, 1); // 获取星期几(英文)

String weekday = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);

另一种写法

 //造一个hash表,里面对应笔画数
        int[] hash = {13, 1, 2, 3, 5, 4, 4, 2, 2, 2};
        //month里面放每月的个数
        int[] months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        //计算打篮球的天数
        int count = 0;
        boolean feb = true;
        for (int year = 2000; year <= 2024; year++) {
            //笔画数
            int num = 0;
            //判断是平年还是润年
            //计算2月的值
            months[2] = isLeapYear(year) ? 29 : 28;
            //计算每一月,每一天
            for (int month = 1; month <= 12; month++) {
                for (int day = 1; day < months[month]; day++) {
                    //计算年的每一位
                    int ypos1 = year / 1000;
                    year = year % 1000;
                    int ypos2 = year / 100;
                    year = year % 100;
                    int ypos3 = year / 10;
                    year = year % 10;
                    int ypos4 = year;
                    //计算月的每一位
                    int mpos1 = month / 10;
                    month = month % 10;
                    int mpos2 = month;
                    //计算每一天的每一位
                    int dpos1 = day / 10;
                    day = day % 10;
                    int dpos2 = day;
                    //计算笔画数
                    num = hash[ypos1] + hash[ypos2] + hash[ypos3] + hash[ypos4] +
                            hash[mpos1] + hash[mpos2] + hash[dpos1] + hash[dpos2];
                    //最后计算天数
                    if (num > 50) {
                        count++;
                    }
                    if (year == 2024 && month == 4 && day == 13) {
                        System.out.println(count);
                        return;

                    }
                }
            }
        }

    }
    public static void main(String[] args) {
        cal();

    }

相关文章:

  • 【Tauri2】010——菜单menu(1)
  • 电脑基础之excel基础操作
  • 网络攻防快速入门笔记pwn | 02 栈溢出题型 | 2.1 ret2text和ret2shellcode
  • 鸿蒙Next-开发版本升级,API升级(例如API12升API16)
  • 前端界面在线excel编辑器 。node编写post接口获取文件流,使用传参替换表格内容展示、前后端一把梭。
  • Django学习笔记
  • 第二章:基础页面实现 - 第一节:登录与注册页面 - 表单与身份验证UI
  • 飞腾派OS(无桌面版本基于Debian11)安装weston桌面及Qt
  • LLM应用层推荐 -- 基于文档的问答tools Web UI 框架 开源向量库 -- 推荐、对比
  • 飞速(FS)HPC无损组网:驱动AI高性能计算网络转型升级
  • Qt笔记----》不同环境程序打包
  • 【docker】将docker容器中的文件复制到宿主机的方法
  • centos线程数查看
  • Typora 小乌龟 git 上传到gitee仓库教程
  • git push失败的解决办法
  • 北斗导航 | THE GNSS AMBIGUITY RATIO-TEST REVISITED: A BETTER WAY OF USING IT【论文要点】
  • linux-core分析-柔性数组越界访问
  • c++中int、float、double类型数据与string类型数据相互转换
  • 一文掌握 Velox orderby 算子的排序算法
  • AWS S3 和 Lambda 使用
  • 我驻苏丹使馆建议在苏中国公民尽快撤离
  • 万达电影:股东杭州臻希拟减持不超1.3927%公司股份
  • 新华时评:任凭风云变幻,中俄关系从容前行
  • 安徽六安原市长潘东旭,已任省市场监督管理局党组书记、局长
  • 太空摄影的发展
  • 中方对中美就关税谈判的立场发生变化?外交部:中方立场没有任何改变