从1900年1月1日(星期一),开始经过的n年当中,没个月的13号这一天是星期一,星期二,星期三...星期日的次数分别是多少

输入1,输出1,3,1,2,2,2,1

package com.neu.test13;

import java.util.Scanner;

public class test04 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        int[] a = new int[7];
        int week = 6;
        for (int i = 1900; i < 1900 + n; i++) {
            for (int j = 1; j <= 12; j++) {
                a[week % 7]++;
                week += get(i, j);
            }
        }
        for (int i = 1; i <= 6; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.print(a[0] + " ");
    }
    public static int get(int y, int m) {
        int i;
        if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
            i = 1;
        else
            i = 0;
        if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
            return 31;
        else if (m == 2) {
            if (i == 1)
                return 29;
            else
                return 28;
        }
        return 30;
    }

}
点赞