【蓝桥杯】航班时间

小 h 前往美国参加了蓝桥杯国际赛。

小 h 的女朋友发现小 h 上午十点出发,上午十二点到达美国,于是感叹到“现在飞机飞得真快,两小时就能到美国了”。

小 h 对超音速飞行感到十分恐惧。

仔细观察后发现飞机的起降时间都是当地时间。

由于北京和美国东部有 12 小时时差,故飞机总共需要 14 小时的飞行时间。

不久后小 h 的女朋友去中东交换。

小 h 并不知道中东与北京的时差。

但是小 h 得到了女朋友来回航班的起降时间。

小 h 想知道女朋友的航班飞行时间是多少。

对于一个可能跨时区的航班,给定来回程的起降时间。

假设飞机来回飞行时间相同,求飞机的飞行时间。

输入格式

一个输入包含多组数据。

输入第一行为一个正整数 T,表示输入数据组数。

每组数据包含两行,第一行为去程的起降时间,第二行为回程的起降时间。

起降时间的格式如下:

  1. h1:m1:s1 h2:m2:s2
  2. h1:m1:s1 h3:m3:s3 (+1)
  3. h1:m1:s1 h4:m4:s4 (+2)

第一种格式表示该航班在当地时间h1时m1分s1秒起飞,在当地时间当日h2时m2分s2秒降落。

第二种格式表示该航班在当地时间h1时m1分s1秒起飞,在当地时间次日h2时m2分s2秒降落。

第三种格式表示该航班在当地时间h1时m1分s1秒起飞,在当地时间第三日h2时m2分s2秒降落。

输出格式

对于每一组数据输出一行一个时间hh:mm:ss,表示飞行时间为hh小时mm分ss秒。

注意,当时间为一位数时,要补齐前导零,如三小时四分五秒应写为03:04:05。

数据范围

保证输入时间合法(0≤h≤23,0≤m,s≤590≤h≤23,0≤m,s≤59),飞行时间不超过24小时。

输入样例:

3
17:48:19 21:57:24
11:05:18 15:14:23
17:21:07 00:31:46 (+1)
23:02:41 16:13:20 (+1)
10:19:19 20:41:24
22:19:04 16:41:09 (+1)

输出样例:

04:09:05
12:10:39
14:22:05

思路:

        假设甲地的时间比乙地慢x, 如果甲地的时间为t1,乙地的时间为t2,则 t1 + x 与 t2 的时间相同(以乙地的时间为标准时间)。

        如果甲地(t1)——>乙地(t2),飞行时间为 t2 - (t1 + x) = t2 - t1 - x = t

        如果乙地(t4)——>甲地(t3),飞行时间为 (t3 + x) - t4 = t3 - t4  + x = t

                                        两式相加 2t = t2 - t1 + t3 - t4

        则所求的飞行时间为给出的两个起飞时间与到达时间作差后相加除以二。

数据处理:

        因为有(+x)的问题,导致数据不能用(scanf(“%d%d:%d%d:%d%d”))统一处理,但是可以从中找到一定的规律从而处理数据。

        x的范围问题,题目中给出飞行时间不超过24小时,所以x不可能是两位数(飞机不可能隔几十天到达目的地)—— 那么 x 的位置一定是可以被锁定的。所以那么我们可以通过读入字符串处理这些数据:

        x的位置

                因为x只有一位,根据题目给出的输入,x一定在数组的第21个位置(即str[20])。以上的情况只适用于(+x)存在的情况,所以还需要判断字符串的长度来判断是否存在(+x)的情况:当字符串的长度为 17 时,不存在隔天到达的情况,当字符串长度大于17时,有隔天到达的情况。

        其他的位置:

                其他的位置相对固定,可以统一处理,具体见代码。

        时间的处理:

                统一将时间处理为秒,秒计算完以后再换算为时、分、秒。计算方法:

                设 总共的时间为 x s。

                时的换算 : h = x / 3600

                分的换算 : m = x / 60 % 60

                秒的换算 : s = x % 60

思路实现:

1.从字符串中获得时间参数

int get_time(string str)
{
    int time1 = 0, time2 = 0;
    int len = str.size();
    if(len > 17) time2 += (str[20] - '0') * 3600 * 24;
    time1 = ( (str[0] - '0') * 10 + (str[1] - '0') ) * 3600 + ( (str[3] - '0') * 10 + (str[4] - '0') ) * 60 + (str[6] - '0') * 10 + (str[7] - '0');
    time2 += ( (str[9] - '0') * 10 + (str[10] - '0') ) * 3600 + ( (str[12] - '0') * 10 + (str[13] - '0') ) * 60 + (str[15] - '0') * 10 + (str[16] - '0');
    return time2 - time1;
}

2.时间的处理

printf("%02d:%02d:%02d\n", t / 3600, t / 60 % 60, t % 60);

完整代码(C++):

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>

using namespace std;

int get_time(string str)
{
    int time1 = 0, time2 = 0;
    int len = str.size();
    if(len > 17) time2 += (str[20] - '0') * 3600 * 24;
    time1 = ( (str[0] - '0') * 10 + (str[1] - '0') ) * 3600 + ( (str[3] - '0') * 10 + (str[4] - '0') ) * 60 + (str[6] - '0') * 10 + (str[7] - '0');
    time2 += ( (str[9] - '0') * 10 + (str[10] - '0') ) * 3600 + ( (str[12] - '0') * 10 + (str[13] - '0') ) * 60 + (str[15] - '0') * 10 + (str[16] - '0');
    return time2 - time1;
}

int main()
{
    int n;
    string str;
    cin >> n;
    getline(cin, str);
    while(n--)
    {
        getline(cin, str);
        int time1 = get_time(str);
        getline(cin, str);
        int time2 = get_time(str);
        
        int t = (time1 + time2) / 2;
        
        printf("%02d:%02d:%02d\n", t / 3600, t / 60 % 60, t % 60);
    }
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dkl2024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值