寒假每日一题day12——ACWing1341. 十三号星期五

该博客内容涉及一个编程问题,即计算从1900年开始,在指定年份内每个月13号是星期五的次数。程序通过枚举年份和月份,结合特定的日期计算规则(如平年和闰年的2月天数),统计13号落在星期五的总次数,并使用了Java进行实现。
摘要由CSDN通过智能技术生成

十三号星期五

【题目描述】
在这里插入图片描述

1341. 十三号星期五

月份口诀巧记:

一三五七八十腊(12月),
三十一天永不zhi差;
四六九冬(11月)三十日;
平年二月二十八,闰年二月把一加。

1 3 5 7 8 10 12 —> 31天
4 6 9 11 —> 30天
2 -->28(平)/29(润)

【思路】
枚举每一年每一月的13号是从1900年1月1日开始的第几天。

import java.io.*;
class Main{
    static int [] cnt = new int[7];
    //除第一年的1月份是加13(再特判) 其他月份的13号都是加上前一个月的天数
    //依次为1月到12月所需加的天数
    static int days[] ={31,31,0,31,30,31,30,31,31,30,31,30};
    public static int isRun(int y){
        if( (y % 4 == 0 && y % 100 !=0)||( y % 400 == 0) )return 29;
        else return 28 ;
    }
    public static void solve(int n){
        //计算每个月的13号是从1900年1月1日开始算起的第几天
        int res = 0;
        for(int y = 0; y < n; y++){
            for(int m = 1; m <= 12; m++){
                if( y==0 && m == 1) res += 13; //第一年的第一个月
                else if( m == 3 ) res += isRun(1900+y);     //2月特判
                else if( y == n-1 && m == 12 ) res += 13;  //最后一年的最后一个月要特判一下
                else res += days[ m-1 ];
                cnt [ res%7 ] ++;
            }
        }
       
    }
    public static void main(String  args[]) throws Exception{
        BufferedReader  bf = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(bf.readLine());
        //cnt 存储顺序是:星期天 星期一 星期二……星期六
        solve(N);
 		for(int i = 6, j = 0 ; j < 7; i = (i+1)%7,j++)
            	System.out.print(cnt[i]+" ");
        // 关闭输入流
        bf.close();
        
    }
}

快速读入

import java.io.*;
要抛出异常。

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
String[] arrStr = reader.readLine().split(" ");
for (int i = 0; i < n; i++)
    arr[i] = Integer.parseInt(arrStr[i]);

// 打印结果
System.out.println(mergeSort(0, n - 1));
// 关闭输入流
reader.close();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值