蓝桥杯官网填空题(含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 小时前
每天学一个算法--LSM-Tree(Log-Structured Merge Tree)
java·算法·lsm-tree
浅念-8 小时前
刷穿LeetCode:BFS 解决 Flood Fill 算法
数据结构·c++·算法·leetcode·职场和发展·bfs·宽度优先
做cv的小昊9 小时前
【TJU】研究生应用统计学课程笔记(8)——第四章 线性模型(4.1 一元线性回归分析)
笔记·线性代数·算法·数学建模·回归·线性回归·概率论
贾斯汀玛尔斯10 小时前
每天学一个算法--倒排索引(Inverted Index)
算法·inverted-index
小e说说10 小时前
打破偏科困境:这些学习软件助孩子重燃学习热情
算法
我命由我1234510 小时前
程序员的心理学学习笔记 - 空杯心态
经验分享·笔记·学习·职场和发展·求职招聘·职场发展·学习方法
月昤昽10 小时前
autoCAD二次开发 4.正多边形与collection区分
算法·c#·二次开发·autocad二次开发
休息一下接着来11 小时前
C++ 固定容量环形队列实现
c++·算法
im_AMBER11 小时前
手撕hot100之矩阵!看完这篇就AC~
javascript·数据结构·线性代数·算法·leetcode·矩阵