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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值