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

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

解题思路:

  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-11 11:24
大家还是用ai改吧,我心疼得要死,就当花钱买教训吧,人家直接拿完钱就跑路了
程序员小白条:简历修改700....神奇,又不是帮你面试,咋的,简历修改从双非变92了还是没实习变成有大厂实习了
点赞 评论 收藏
分享
苍蓝星上艾露:这简历。。。可以试试我写的开源简历优化工具https://github.com/weicanie/prisma-ai
点赞 评论 收藏
分享
昨天 14:14
门头沟学院 Java
7.10投递7.15感谢信
投递地平线等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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