编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#46655 #112. czq的时间间隔 Accepted 100 1063 ms 31760 K Java / 3.5 K 计试001-顾真榕 2020-10-18 15:53:08
显示原始代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < n; i++) {
            MyDate a, b;
            String dateOneRawPattern = sc.nextLine();
            try {
                a = new MyDate(Integer.parseInt(dateOneRawPattern.split("/")[0]),
                    Integer.parseInt(dateOneRawPattern.split("/")[1]),
                    Integer.parseInt(dateOneRawPattern.split("/")[2].split(" ")[0]),
                    Integer.parseInt(dateOneRawPattern.split(" ")[1].split(":")[0]),
                    Integer.parseInt(dateOneRawPattern.split(" ")[1].split(":")[1]),
                    Integer.parseInt(dateOneRawPattern.split(" ")[1].split(":")[2]));
                b = new MyDate(Integer.parseInt(dateOneRawPattern.split(" ")[2].split("/")[0]),
                    Integer.parseInt(dateOneRawPattern.split(" ")[2].split("/")[1]),
                    Integer.parseInt(dateOneRawPattern.split(" ")[2].split("/")[2].split(" ")[0]),
                    Integer.parseInt(dateOneRawPattern.split(" ")[3].split(":")[0]),
                    Integer.parseInt(dateOneRawPattern.split(" ")[3].split(":")[1]),
                    Integer.parseInt(dateOneRawPattern.split(" ")[3].split(":")[2]));
                System.out.println(MyDate.deltaSec(b, a));
            } catch (Exception e) {
                System.out.println("-1");
            }
        }
        sc.close();
    }
}

final class MyDate {
    public long seconds;
    private static final int[] daysSumLeap = { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335,
        366 };
    private static final int[] daysSumNormal = { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334,
        365 };

    private static boolean isLeapYear(int year) {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

    private static boolean validateDate(int year, int month, int day, int hr, int min, int sec) {
        if (year <= 0)
            return false;
        if (month <= 0 || month >= 13)
            return false;
        if (day <= 0)
            return false;
        if (isLeapYear(year) && day > daysSumLeap[month + 1] - daysSumLeap[month])
            return false;
        if (!isLeapYear(year) && day > daysSumNormal[month + 1] - daysSumNormal[month])
            return false;
        if (hr < 0 || hr > 23)
            return false;
        if (min < 0 || min > 59)
            return false;
        if (sec < 0 || sec > 59)
            return false;
        return true;
    }

    public MyDate(int year, int month, int day, int hr, int min, int sec) throws Exception {
        if (!validateDate(year, month, day, hr, min, sec))
            throw new Exception("Invalid format.");
        this.seconds = (long) year * 31536000;
        long leapSeconds = 0;
        for (int i = 0; i < year; i++)
            if (isLeapYear(i))
                leapSeconds += 86400;
        if (isLeapYear(year))
            this.seconds += daysSumLeap[month] * 86400;
        else
            this.seconds += daysSumNormal[month] * 86400;
        this.seconds += (day - 1) * 86400 + leapSeconds + hr * 3600 + min * 60 + sec;
    }

    public static long deltaSec(MyDate date1, MyDate date2) {
        return date1.seconds > date2.seconds ? date1.seconds - date2.seconds : date2.seconds - date1.seconds;
    }
}
子任务 #1
Accepted
得分:100
测试点 #1
Accepted
得分:100
用时:200 ms
内存:15732 KiB

输入文件(1.in

14
2020/08/22 23:48:00 2020/08/22 23:48:59
2020/08/22 23:48:59 2020/08/22 23:48:00
2020/08/22 23:22:
<463 bytes omitted>

答案文件(1.out

59
59
1537
82527
1032927
21164127
17902896185
-1
-1
-1
-1
-1
-1
315537897599

用户输出

59
59
1537
82527
1032927
21164127
17902896185
-1
-1
-1
-1
-1
-1
315537897599

系统信息

Exited with return code 0
测试点 #2
Accepted
得分:100
用时:863 ms
内存:31760 KiB

输入文件(2.in

10000
5348/13/26 20:09:36 5809/10/09 11:17:33
8951/13/09 02:54:35 1245/10/20 19:37:36
7524/01/04 20:
<399906 bytes omitted>

答案文件(2.out

-1
-1
52181193949
167527773820
11151421677
176367406636
34137847390
71087537213
156479412870
<102130 bytes omitted>

用户输出

-1
-1
52181193949
167527773820
11151421677
176367406636
34137847390
71087537213
156479412870
-1
-1
81508312928
163816674890
-1
6
<92102 bytes omitted>

系统信息

Exited with return code 0