蓝桥杯航班时间

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

蓝桥杯其他真题点这里

//飞行时间 - 时差 = 已过去的时间1
//飞行时间 + 时差 = 已过去的时间2
//两个式子相加会发现 飞行时间 = 两段时间差的和 >> 1

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


public class Main{
    static int n;
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args)throws IOException {
        n = Integer.parseInt(in.readLine());
        while (n -- > 0){
            
            //读取两个输入数据,每个输入数据里都含有出发点出发时间,目标地落地时间跨越天数
            String t1 = in.readLine();
            String t2 = in.readLine();

            //两段时间差的和 >> 1
            int time = get_time(t1) + get_time(t2) >> 1;
            int hour = time / 3600;
            int minute = time % 3600 / 60;
            int second = time % 60;
            System.out.printf("%02d:%02d:%02d\n",hour,minute,second);
        }
        in.close();
    }

    //计算两段时间的差
    public static int get_time(String t){
        //统一格式,没有跨越天数就加上" (+0)"
        if (t.charAt(t.length() - 1) != ')') t += " (+0)";

        //将时间分成三段,有出发点出发时间,目标地落地时间跨越天数
        String[] time = t.split(" ");
    
        //出发点出发时间的时分秒
        String[] start = time[0].split(":");
        int h1 = Integer.parseInt(start[0]);
        int m1 = Integer.parseInt(start[1]);
        int s1 = Integer.parseInt(start[2]);

        //目标点落地时间的时分秒
        String[] arrive = time[1].split(":");
        int h2 = Integer.parseInt(arrive[0]);
        int m2 = Integer.parseInt(arrive[1]);
        int s2 = Integer.parseInt(arrive[2]);

        //跨越天数
        int d = time[2].charAt(2) - '0';

        //计算两端时间的差
        return get_seconds(h2,m2,s2) - get_seconds(h1,m1,s1) + d * 3600 * 24;
    }

    //将时间转成秒来计算会比较方便
    public static int get_seconds(int h,int m,int s){
        return h * 3600 + m * 60 + s;
    }
}

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6