计算日期到天数转换, 华为

判断闰年:4年闰, 100年不闰, 400 年再闰。


import java.util.*;
public class Main {
    static boolean isLeapYear(int year) {
        if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) return true;
        return false;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int year = sc.nextInt();
            int month = sc.nextInt();
            int day = sc.nextInt();
            int[] days = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            if(year < 0) System.out.println(-1);
            else {
                if(isLeapYear(year))
                    days[2] ++;
                if(month < 1 || month > 12) System.out.println(-1);
                else {
                    if(day < 1 || day > days[month]) System.out.println(-1);
                    else {
                        int res = 0;
                        for(int i=1; i < month; i++) res += days[i];
                        res += day;
                        System.out.println(res);
                    }
                }
            }
            
        }
    }
}
posted @ 2020-07-06 22:51  li修远  阅读(405)  评论(0编辑  收藏  举报