题解 | 计算日期到天数转换

#牛客创作赏金赛# #刷题我是认真的#

解题思路:

  1. 思路比较简单: 存储一个每个月份有多少的天的数组,累加即可
  2. 特殊情况:
  3. 闰年的判断:
  4. 普通闰年: 普通年份能被 4 整除但不能被 100 整除的为闰年
  5. 世纪闰年:世纪年能被 400 整除的是闰年,并且被100整除

b. 如果闰年就增加一天,但前提是月份的值大于2

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] nums = in.nextLine().split(" ");
        int[] monthDays = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int year = Integer.parseInt(nums[0]);
        int month = Integer.parseInt(nums[1]);
        int day = Integer.parseInt(nums[2]);
        int total = 0;
        for (int i = 0; i < month - 1; i++) {
            total += monthDays[i];
        }
        if (year % 100 != 0 && year % 4 == 0 && month > 2) {
            total += 1;
        }
        if (year % 100 == 0 && year % 400 == 0 && month > 2) {
            total += 1;
        }
        total += day;
        System.out.println(total);
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 13:05
TMD找工作本来就烦,这东西什么素质啊😡
Beeee0927:hr是超雄了,不过也是有道理的
点赞 评论 收藏
分享
06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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