普通闰年(y%4 == 0 && y%100 != 0),世纪闰年(y%400 == 0)

计算一年中的第几天

http://www.nowcoder.com/questionTerminal/178aa3dafb144bb8b0445edb5e9b812a

普通闰年(y%4 == 0 && y%100 != 0),世纪闰年(y%400 == 0):

import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int[] a = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    //平年
        int[] b = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    //闰年
        while(sc.hasNextLine()){
            String[] s = sc.nextLine().split(" ");
            int y = Integer.parseInt(s[0]);
            int m = Integer.parseInt(s[1]) - 1;
            int d = Integer.parseInt(s[2]);
            int sum = 0;
            int[] v;
            if((y%4 == 0 && y%100 != 0)||(y%400 == 0)) v = b;    //闰年
            else v = a;
            for(int i = 0; i < m; ++i) sum += v[i];    //各月天数直接累加即可
            sum += d;
            System.out.println(sum);
        }
    }
}
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务