蓝桥杯官网填空题(含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;
    }
}
相关推荐
Fuyo_11191 天前
C++ AVL树保姆级详解:从平衡因子到四大旋转机制
java·c++·算法
jktantan1 天前
每个节点 41 字节:把一棵百万节点树拆进 14 个 typed array
前端·javascript·vue.js·算法·typescript
揽星河..1 天前
【C语言】猜数字游戏
c语言·算法·游戏
frjc1 天前
分布式服务拆分实战指南:从底层逻辑到面试话术
微服务·面试·职场和发展
LabVIEW开发1 天前
CTM-3 红外测温仪 + LabVIEW 温度怎么测?
算法·labview·测试·labview程序
纪念 2291 天前
C++算法(六)
开发语言·c++·算法
Dumbness1 天前
UIA 读微信群消息没有 id,怎么判断哪条是新的:后缀对齐在翻历史和撤回时整屏重报
python·算法
taka1 天前
从 24x 到 141x:我用纯 Python 零依赖项目,扒开了 GPU 加速的性能黑洞
算法
UIU1141 天前
P16237 [蓝桥杯 2026 省 B] 应急布线
数据结构·c++·算法·蓝桥杯
旖旎夜光1 天前
LeetCode 315: 算右侧小于当前元素的个数(归并排序) —— 题解
c++·学习·算法·leetcode·排序算法