蓝桥杯航班时间

蓝桥杯其他真题点这里👈

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;
    }
}
相关推荐
码出财富7 小时前
SpringBoot 内置的 20 个高效工具类
java·spring boot·spring cloud·java-ee
优雅的潮叭7 小时前
c++ 学习笔记之 shared_ptr
c++·笔记·学习
多米Domi0117 小时前
0x3f第33天复习 (16;45-18:00)
数据结构·python·算法·leetcode·链表
我是小疯子667 小时前
Python变量赋值陷阱:浅拷贝VS深拷贝
java·服务器·数据库
am心7 小时前
学习笔记-用户下单
笔记·学习
森叶7 小时前
Java 比 Python 高性能的原因:重点在高并发方面
java·开发语言·python
二哈喇子!7 小时前
Eclipse中导入外部jar包
java·eclipse·jar
微露清风7 小时前
系统性学习C++-第二十二讲-C++11
java·c++·学习
罗湖老棍子7 小时前
【例4-11】最短网络(agrinet)(信息学奥赛一本通- P1350)
算法·图论·kruskal·prim
方圆工作室8 小时前
【C语言图形学】用*号绘制完美圆的三种算法详解与实现【AI】
c语言·开发语言·算法