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

日期差值的计算

1、枚举所有数值进行日期判断

时间复杂度是o(n)的,比较慢,单实例能凑合用,多实例的话时间复杂度有点高。

核心代码就是判断某个八位数能否表示一个日期。

	static int[] month = {0,31,28,31,30,31,30,31,31,30,31,30,31};static String a, b;    static void sovle() throws Exception {b = br.readLine();int s = 0;int l = Integer.parseInt(a), r = Integer.parseInt(b);for(int i = l; i <= r; i ++)    if(check(i))s ++;bw.write(s + "\n");}private static boolean check(int i) {int a = i / 10000, b =  i / 100 % 100, c = i % 100;if(b < 1 || b > 12 || c < 1) return false;if(b == 2 && asd(a)) return c <= 29;return c <= month[b];}private static boolean asd(int a) {return a % 400 == 0 || a % 4 == 0 && a % 100 != 0;}    public static void main(String args[]) throws Exception {
//		int t = Integer.parseInt(br.readLine());int t = 1;while((a = br.readLine()) != null) {sovle();}bw.flush();bw.close();}

2、模拟日期的加法

时间复杂度为o(n),比第一个方法稍微快一点,但是这里的n是合法日期的数量。

    static int[] month = {0, 31,28,31,30,31,30,31,31,30,31,30,31};static String a, b;static void sovle() throws Exception {b = br.readLine();int s = 0;int l = Integer.parseInt(a), r = Integer.parseInt(b);while(l <= r) {s ++;l = ne(l);}bw.write(s + "\n");}private static int ne(int i) {i = i + 1;int a = i / 10000, b =  i / 100 % 100, c = i % 100;if(b == 2 && asd(a)) {if(c > 29) {b = 3;c = 1;}} else {if(c > month[b]) {b ++;c = 1;}}if(b == 13) {a ++;b = 1;}return a * 10000 + b * 100 + c;}private static boolean asd(int a) {return a % 400 == 0 || a % 4 == 0 && a % 100 != 0;}public static void main(String args[]) throws Exception {
//		int t = Integer.parseInt(br.readLine());int t = 1;while((a = br.readLine()) != null) {sovle();}bw.flush();bw.close();}

3、枚举整年、整月

时间复杂度是 o(n),这里的n是年份,最高才10000;时间是最快的,基本可以满足所有情况下的使用。

	static int[] month = {0, 31,28,31,30,31,30,31,31,30,31,30,31};static String a, b;	static void sovle() throws Exception {b = br.readLine();int s = 0;int l = Integer.parseInt(a), r = Integer.parseInt(b);if(l > r) {int a = l;l = r;r = a;}s = get(l, r);bw.write(s + "\n");}private static int get(int l, int r) {int s = 0;int a1 = l / 10000, b1 =  l / 100 % 100, c1 = l % 100;int a2 = r / 10000, b2 =  r / 100 % 100, c2 = r % 100;for(int i = a1; i < a2; i ++) {if(asd(i)) s += 366;else s += 365;}for(int i = 1; i < b2; i ++) {s += month[i];if(i == 2 && asd(a2)) s ++;}s += c2;for(int i = 1; i < b1; i ++) {s -= month[i];if(i == 2 && asd(a1)) s --;}s -= c1;s += 1;return s;}private static boolean asd(int a) {return a % 400 == 0 || a % 4 == 0 && a % 100 != 0;}public static void main(String args[]) throws Exception {
//		int t = Integer.parseInt(br.readLine());int t = 1;while((a = br.readLine()) != null) {sovle();}bw.flush();bw.close();}

http://www.lryc.cn/news/320620.html

相关文章:

  • 为什么需要Occupancy?
  • SSA优化最近邻分类预测(matlab代码)
  • nginx相关内容的安装
  • 基于SpringBoot和Echarts的全国地震可视化分析实战
  • 基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的农作物害虫检测系统(深度学习模型+UI界面+训练数据集)
  • 21 # 高级类型:条件类型
  • Java之List.steam().sorted(Comparator.comparing())排序异常解决方案
  • js判断对象是否有某个属性
  • CleanMyMac X2024永久免费的强大的Mac清理工具
  • 等保测评的知识
  • 【算法】多路归并(鱼塘钓鱼)
  • unity3d Animal Controller的Animal组件中General基础部分理解
  • css背景从上到下颜色渐变、css背景从左到右颜色渐变、 css框线展示外阴影、css框线展示内阴影
  • Nacos学习笔记
  • 微信小程序 nodejs+vue+uninapp学生在线选课作业管理系统
  • trpc-go 博客系统
  • 【JAVA】Servlet开发
  • k8s helm 删除 tiller
  • Python入门(小白友好)
  • 【数据结构与算法】:非递归实现快速排序、归并排序
  • 2024-3-18-C++day6作业
  • 【OceanBase诊断调优】—— 敏捷诊断工具obdiag一键分析OB集群日志设计与实践
  • python 调用redis创建查询key
  • 归并排序思路
  • 【蓝桥杯选拔赛真题65】python输出三个字符 第十五届青少年组蓝桥杯python选拔赛真题 算法思维真题解析
  • K8S日志收集方案-EFK部署
  • js基础语法大全(时间戳,uuid,字符串转json)
  • uView LoadingIcon 加载动画
  • Elasticsearch使用Kibana进行基础操作
  • “SRP模型+”多技术融合在生态环境脆弱性评价模型构建、时空格局演变分析与RSEI 指数的生态质量评价及拓展应用教程