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

题目描述

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

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

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

运行限制

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

① 超时了...

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;
    }*/
}

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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值