普通闰年(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);
        }
    }
}
全部评论

相关推荐

07-01 17:14
中北大学 Java
兄弟们是真是假
牛客46374834...:我在boss上投java岗从来没成功过
点赞 评论 收藏
分享
05-26 16:13
门头沟学院 C++
牢大肘击Java:海投就完事了bro,就当刷视频了
点赞 评论 收藏
分享
06-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务