P103 日期问题

这道题只需要暴力枚举每一个情况就可以

代码奉上

java 复制代码
import java.util.Scanner;

public class Main {
    static int[] days = {0,31,28,31,30,31,30,31,31,30,31,30,31};
     static boolean check(int Y , int M , int D) {
        if (M == 0 || M > 12) return false;
        if (D == 0 || M != 2 && D > days[M]) return false;
        if (M == 2) {
            int leap = Y % 100 != 0 && Y % 4 == 0 || Y % 400 == 0 ? 1 : 0;
            if (D > 28 + leap) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        //将字符串转换为数字
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String[] parts = input.split("/");
        int AA = Integer.parseInt(parts[0]);
        int BB = Integer.parseInt(parts[1]);
        int CC = Integer.parseInt(parts[2]);

        for (int i = 19600101; i <= 20591231; i++) {
            int year = i / 10000;
            int Month = i / 100 % 100;
            int day = i % 100;

            if (check(year, Month, day)) {
                if (year % 100 == AA && Month == BB && day == CC || year %100 == BB && Month == CC && day == AA || year %100 == CC && Month == AA && day == BB) {
                    System.out.printf("%02d-%02d-%02d\n", year, Month, day);
                }
            }
        }
    }
}

没有什么好解释的,主要想说一下paresInt和valueOf的区别

我们要首先说一下基本类型和对象的区别:

基本类型 = 纯数值,光秃秃,啥功能没有 对象 = 装数值的 "盒子",有功能、有方法、有属性

除了那8个基本类型,剩下的都是对象

基本类型:

  • 只有值,没有任何方法 你不能写 a.xxx,会报错
  • 存的就是数字本身
  • 速度极快
  • 不能放进集合(List、Map)
  • 赋值就是复制值 int b = a; 复制的是 10 这个值

对象:

  • 是一个 "东西",有方法、有功能
  • 存的是地址(引用)
  • 可以放进集合
  • 赋值是复制地址

基本类型 = 一张钞票(只有面值,没功能)

对象 = 一个钱包(里面有钱,还能打开、存放)

现在说一下paresInt和valueOf的区别:

Integer.parseInt(s) → 得到 基本类型 int

Integer.valueOf(s) → 得到 对象 Integer

什么时候用哪个?

parseInt

  • 做计算

  • 存到 int 变量里

  • 90% 的日常情况都用它

valueOf

  • 要放进 集合(List、Map)

  • 需要 对象

相关推荐
xin_nai8 小时前
判断质数(Java版)
算法
我星期八休息8 小时前
Python-基础语法大全
开发语言·python
源代码杀手8 小时前
利用MATLAB®和Simulink®资源的可再生能源
开发语言·matlab
请数据别和我作队8 小时前
Python实现直播弹幕数据采集(WebSocket实时弹幕采集)
开发语言·网络·python·websocket·网络协议·学习分享
biter down8 小时前
C++11 可变参数模板
开发语言·c++
无心使然云中漫步8 小时前
ArcGis常用服务介绍及Arcgis,Openlayers,Leaflet加载
开发语言·arcgis·php
锦瑟弦音8 小时前
Java与SQL基础知识总结
java·开发语言
停水咋洗澡8 小时前
Redis Sentinel高可用实战:主从自动故障转移
java·redis·sentinel
大黄说说8 小时前
React Hooks 与 Class Components 的本质区别:从“面向对象”到“函数式”的范式转移
开发语言
ch.ju8 小时前
Java程序设计(第3版)第二章——引用数据类型:String
java