7-星期几(蓝桥杯)

题目描述

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

19491949 年的国庆节( 1010 月 11 日)是星期六。

今年(20122012)的国庆节是星期一。

那么,从建国到现在,有几次国庆节正好是星期日呢?

不要求写出具体是哪些年,只要一个数目!

运行限制

  • 最大运行时间:1s

  • 最大运行内存: 128M

源码:

package secondTest;

public class 星期几 {
    static int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    static int year = 1949;
    static int month = 10;
    static int day = 1;
    static int t = 6;
    public static void main(String[] args) {
        //1949 年的国庆节( 1010 月 11 日)是星期六。
        //今年(20122012)的国庆节是星期一。
        long res = 0;
        while(year != 2012 || month != 12 || day != 31) {
            if(year % 400 == 0 ||(year % 4 == 0 && year % 100 != 0)) {
                days[2] = 29;
            }else {
                days[2] = 28;
            }
            if(check()) {
                res++;
            }
            day++;
            t++;
            t = t % 7;
            if(day > days[month]) {
                month++;
                day = 1;
            }
            if(month > 12) {
                year++;
                month = 1;
            }
        }
        if(check()) {
            res++;
        }
        System.out.println(res);
    }
    private static boolean check() {
        // TODO Auto-generated method stub
        int y = year;
        int m = month;
        int d = day;
        if(month==10&&day==1&&t==0) {
            return true;
        }
        return false;
    }
}

正确答案9

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月亮被咬碎成星星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值