第一篇文章...?

计算日期到天数转换

http://www.nowcoder.com/questionTerminal/769d45d455fe40b385ba32f97e7bcded

import java.util.*;

public class Main {
    // 这里1月对应monthDay[1]更符合现实思维
    // 如果用前缀和的思想,下面就不用for循环了...
    private static int[] monthDay = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String[] date = sc.nextLine().split(" ");
            int y = Integer.parseInt(date[0]);
            int m = Integer.parseInt(date[1]);
            int d = Integer.parseInt(date[2]);

            int forFeb = ((y % 400 == 0) | (y % 4 == 0 && y % 100 != 0)) ? 1 : 0;
            int dayCount = (m > 2) ? (forFeb + d) : (d);

            for (int i = 1; i < m; i++) {
                dayCount += monthDay[i];
            }

            System.out.println(dayCount);
        }
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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