蓝桥杯官网填空题(含2天数)

题目描述

本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。

小蓝特别喜欢 2,今年是公元 2020 年,他特别高兴,因为每天日历上都可以看到 2。

如果日历中只显示年月日,请问从公元 1900 年 1 月 1 日到公元 9999 年 12 月 31 日,一共有多少天日历上包含 2。即有多少天中年月日的数位中包含数字

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

① 超时了...

java 复制代码
public class Main {
    public static void main(String[] args) {
        /*int year=1900,month=1,day=1;
        String str;
        int ans=0;
        int[] days=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        while(true){
            str="";
            if(year%400==0||(year%4==0&&year%100!=0)){
                days[2]=29;
            }
            else{
                days[2]=28;
            }
            str+=String.valueOf(year)+String.valueOf(month)+String.valueOf(day);
            if(check(str)){
                ans++;
            }
            if(year==9999&&month==12&&day==31){
                break;
            }
            day++;
            if(day>days[month]){
                day=1;
                month++;
            }
            if(month>12){
                month=1;
                year++;
            }
        }
        System.out.println(ans);*/
        System.out.println(1994240);
    }
    /*public static boolean check(String s){
        int n=Integer.parseInt(s);
        int y;
        while(n>0){
            y=n%10;
            if(y==2){
                return true;
            }
            n=n/10;
        }
        return false;
    }*/
}

java 复制代码
public class Main {
    public static void main(String[] args) {
        int[] days=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        int ans=0;
        for(int year=1900;year<=9999;year++){
          if(year%400==0||(year%4==0&&year%100!=0)){
                days[2]=29;
            }
            else{
                days[2]=28;
            }
            for(int month=1;month<=12;month++){
              for(int day=1;day<=days[month];day++){
                if(check(year)||check(month)||check(day)){
                  ans++;
                }
              }
            }
        }
        System.out.println(ans);
    }
    public static boolean check(int n){
        int y;
        while(n>0){
            y=n%10;
            if(y==2){
                return true;
            }
            n=n/10;
        }
        return false;
    }
}
相关推荐
菩提树下的凡夫4 分钟前
新版OpenCV5.0在ONNX模型的推理应用
opencv·算法
影寂ldy21 分钟前
C# 三大内置委托(Action / Func / Predicate)+ Lambda
c++·算法·c#
机器学习之心33 分钟前
小龙虾优化算法(COA)驱动的CNN-LSTM多输出回归模型及其SHAP可解释性分析
算法·cnn·lstm·小龙虾优化算法·cnn-lstm多输出回归·shap可解释性分析
阿正的梦工坊44 分钟前
【Rust】13-Trait 系统、动态分发与对象安全
算法·安全·rust
言存1 小时前
力扣热题283 移动零
数据结构·算法·leetcode
字节高级特工1 小时前
智能指针原理与使用场景全解析
开发语言·c++·算法
珊瑚里的鱼1 小时前
【动态规划】买卖股票的最佳时机Ⅲ
算法·动态规划
逻辑星辰1 小时前
x-ds-pow-response逆向分析
开发语言·人工智能·python·深度学习·算法
CQU_JIAKE1 小时前
6.9【aAAA]
算法
Lewiis1 小时前
白话桶排序
数据结构·算法·golang·排序算法