AcWing 1231. 航班时间 (日期时间)

Problem

同样是恶心的时间问题,这个题有点类似初中物理的流水划船问题,正解是两次的时间相加除以2就可以了。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pw = new PrintWriter(System.out);

    public static int get_seconds(int h, int m, int s) {
        return h * 3600 + m * 60 + s;
    }

    public static int get_time(String t1) {
        if (t1.charAt(t1.length() - 1) != ')') t1 += " (+0)";

        String[] t2 = t1.split(" ");
        String[] t3 = t2[0].split(":");
        String[] t4 = t2[1].split(":");
        int h1 = Integer.parseInt(t3[0]);
        int m1 = Integer.parseInt(t3[1]);
        int s1 = Integer.parseInt(t3[2]);

        int h2 = Integer.parseInt(t4[0]);
        int m2 = Integer.parseInt(t4[1]);
        int s2 = Integer.parseInt(t4[2]);
        int d = t2[2].charAt(2) - '0';

        return get_seconds(h2, m2, s2) - get_seconds(h1, m1, s1) + d * 24 * 3600;

    }

    public static void main(String[] args) throws NumberFormatException, IOException {
        int n = Integer.parseInt(br.readLine().trim());
        while (n-- > 0) {
            String s1 = br.readLine();
            String s2 = br.readLine();
            //获取飞行时间,按秒计算
            int time = (get_time(s1) + get_time(s2)) / 2;
            int hour = time / 3600, minute = time % 3600 / 60, second = time % 60;
            pw.printf("%02d:%02d:%02d\n", hour, minute, second);
        }
        pw.flush();
        pw.close();
        br.close();
    }
}
发布了167 篇原创文章 · 获赞 3 · 访问量 3420

猜你喜欢

转载自blog.csdn.net/qq_43515011/article/details/104311876