专题(四) 枚举模拟排序—— AcWing 1231. 航班时间

【题目描述】
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

AcWing 1231. 航班时间

【思路】
这个航班问题有点头疼 没搞明白时间差的计算。

中国使用北京时间,是东8区的区时。 两地相差8-0=8个时区,对应时差是8个小时,北京在东边,比英国早8个小时。也就是英国的晚上8点是中国的第二天的凌晨4点。

北京在东边,比美国早12个小时。北京上午上午十点,是美国的前一天晚上的22:00,第二天上午十二点才到的美国。
所以时间是 12个小时。
即从东往西飞, 飞行时间time为: end1 - (start1 - t ) = end1 - start1 + t
从西往东飞:飞行时间 time为: end2 - (start2 + t) = end2 - start2 - t

中东在中国的东边,所以从中国到中东是从西往东飞,减去时差。根据两次航班时间相同,所以

2*time = end1 - start1 + t + end2 - start2 - t = end1 -start1 + end2 -start2
time =( end1 -start1 + end2 -start2)/2

1、 处理输入,将所有时间转为距离当天00:00:00的秒数,根据time =( end1 -start1 + end2 -start2)/2计算一趟的时间。
2、 将时间分解获得小时、分钟、秒钟。
在这里插入图片描述

import java.io.*;
public class Main{
    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    
    public static int get_second(int h, int m, int s){
        return h * 3600 + m * 60 + s;
    }
    //将所有时间转化为以秒为单位 17:21:07 00:31:46 (+1)
    public static int get_time() throws Exception{
        String line = bf.readLine();
        
        int day = 0;
        int len = line.length();
        if(line.charAt( len - 1) == ')' ){
            day = Integer.parseInt(""+line.charAt(len - 2));
            
        }//11:05:18 15:14:23
        String s[] = line.split(" ");
        String s1[] = s[0].split(":"); // 17 21 07
        String s2[] = s[1].split(":");
        
        int a1 = Integer.parseInt(s1[0]);
        int a2 = Integer.parseInt(s1[1]);
        int a3 = Integer.parseInt(s1[2]);
        int b1 = Integer.parseInt(s2[0]);
        int b2 = Integer.parseInt(s2[1]);
        int b3 = Integer.parseInt(s2[2]);
        
        //System.out.println(a1 +" "+ a2 + " "+a3);
        return (get_second(b1, b2, b3) + day * 3600 * 24) - get_second(a1, a2, a3);
        
    }
    public static void main(String args[]) throws Exception{
        int T = Integer.parseInt(bf.readLine());
        while(T -- > 0){
            int time = (get_time() + get_time()) / 2;
            int hour = time / 3600, minute = time % 3600 / 60,  second = time % 60;
            System.out.printf("%02d:%02d:%02d", hour, minute, second);
            System.out.println();
            
        }
        bf.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值