蓝桥杯航班时间

蓝桥杯其他真题点这里👈

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;
    }
}
相关推荐
石像鬼₧魂石6 分钟前
内网渗透靶场实操清单(基于 Vulhub+Metasploitable 2)
linux·windows·学习·ubuntu
超级大只老咪1 小时前
数组相邻元素比较的循环条件(Java竞赛考点)
java
hh随便起个名1 小时前
力扣二叉树的三种遍历
javascript·数据结构·算法·leetcode
小浣熊熊熊熊熊熊熊丶1 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
毕设源码-钟学长2 小时前
【开题答辩全过程】以 公交管理系统为例,包含答辩的问题和答案
java·eclipse
啃火龙果的兔子2 小时前
JDK 安装配置
java·开发语言
星哥说事2 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
派大鑫wink2 小时前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势
java·spring boot·后端
xUxIAOrUIII2 小时前
【Spring Boot】控制器Controller方法
java·spring boot·后端
Dolphin_Home2 小时前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)
java·spring boot·后端·spring cloud·database·广度优先·图搜索算法