蓝桥杯官网填空题(含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;
    }
}
相关推荐
alphaTao2 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
Interview Aid1122 小时前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
顶点多余11 小时前
那些在算法中适合巩固的知识点---1
java·前端·算法
罗西的思考13 小时前
【Agentic RL / 强化学习框架】Molt 设计解读
人工智能·算法·机器学习
hahaha601613 小时前
HLS高层次综合设计技巧--C++类和模板
图像处理·人工智能·算法·计算机视觉
多弗朗皮卡丘14 小时前
算法详解4:买卖股票的最佳时机系列(上)
算法
维克兜率天17 小时前
【维克】动量指标家族:RSI、ROC、CCI、Momentum全面解析
python·算法
AI情绪识别开源17 小时前
检信 AI 智能推广平台(代号:JX-Promote)
人工智能·算法·erlang
老当益壮梁奶奶18 小时前
Linux软件编程学习笔记(八):进程间通信详解(1)
linux·c语言·笔记·学习·算法
不会就选b18 小时前
算法日常・每日刷题--<BFS拓扑排序>4
算法