蓝桥杯航班时间

蓝桥杯其他真题点这里👈

java 复制代码
//飞行时间 - 时差 = 已过去的时间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;
    }
}
相关推荐
吃好睡好便好1 天前
说说如何爱护肠道
学习·生活
CoreTK_EMC1 天前
牙科医疗器械 ESD 静电整改案例|芯通康医疗级方案,护航诊疗安全与合规
网络·学习·emc整改·芯通康
南浦别a1 天前
第一百一十三天--慢慢改变吧
学习·程序人生
cheems95271 天前
[算法手记] 贪心 爬楼梯问题
算法·贪心算法
KaMeidebaby1 天前
卡梅德生物技术快报|酵母双杂交 cDNA 文库构建与蛋白互作筛选流程
服务器·前端·数据库·人工智能·算法
影寂ldy1 天前
C#Dictionary字典
数据结构
sheeta19981 天前
LeetCode 每日一题笔记 日期:2026.05.27 题目:3121. 统计特殊字母的数量 II
笔记·算法·leetcode
ST——Jess1 天前
年度行业趋势研究报告:泛心理数字化赛道“流日推演”的算法困境与高保真交互范式重构
人工智能·算法·架构
Tisfy1 天前
LeetCode 3300.替换为数位和以后的最小元素:一次遍历
数学·算法·leetcode·模拟
星梦清河1 天前
Java—异步编程
java·开发语言