藍橋杯其他真題點這里👈
//飛行時間 - 時差 = 已過去的時間1
//飛行時間 + 時差 = 已過去的時間2
//兩個式子相加會發現 飛行時間 = 兩段時間差的和 >> 1import 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();//兩段時間差的和 >> 1int 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;}
}