【蓝桥杯】航班时间

航班时间

对于一个可能跨时区的航班,给定来回程的起降时间。假设飞机来回飞行时间相同,求飞机的飞行时间。
输入格式
  从标准输入读入数据。
  一个输入包含多组数据。

输入第一行为一个正整数T,表示输入数据组数。
  每组数据包含两行,第一行为去程的 起降 时间,第二行为回程的 起降 时间。
  起降时间的格式如下

h1:m1:s1 h2:m2:s2
  或
  h1:m1:s1 h3:m3:s3 (+1)
  或
  h1:m1:s1 h4:m4:s4 (+2)
  表示该航班在当地时间h1时m1分s1秒起飞,

第一种格式表示在当地时间 当日 h2时m2分s2秒降落
  第二种格式表示在当地时间 次日 h3时m3分s3秒降落。
  第三种格式表示在当地时间 第三天 h4时m4分s4秒降落。

对于此题目中的所有以 h: m : s 形式给出的时间, 保证 ( 0<=h<=23, 0<=m,s<=59 ).
输出格式
  输出到标准输出。
  对于每一组数据输出一行一个时间hh:mm:ss,表示飞行时间为hh小时mm分ss秒。
  注意,当时间为一位数时,要补齐前导零。如三小时四分五秒应写为03:04:05。
样例输入

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
思路
  • 模拟
  • 设计一个时间类,做有关时间的计算,再根据题目描述模拟即可
  • 注意消除时差,用往返两个的时间之和除以2即可,好比于 t 1 = T − x , t 2 = T + x t_1=T-x,t_2=T+x t1=Txt2=T+x,那么要求 T T T,只需把 t 1 t_1 t1 t 2 t_2 t2 加起来除以2即可,即 T = t 1 + t 2 2 T=\frac{t_1+t_2}{2} T=2t1+t2
代码如下
#include <iostream>
#include <string>
using namespace std;

//时间类
class Time {
    int th, tm, ts;

   public:
    Time(int h = 0, int m = 0, int s = 0) : th(h), tm(m), ts(s) {}
    //字符串构造时间
    //注意题目的标准化输入,每个时间串是固定的8个字符长度
    Time(const string& str) {
        if (str.size() != 8) {
            return;
        }
        th = (str[0] - '0') * 10 + (str[1] - '0');
        tm = (str[3] - '0') * 10 + (str[4] - '0');
        ts = (str[6] - '0') * 10 + (str[7] - '0');
    }

    int get_hour() { return th; }

    void set_hour(int h) { th = h; }

    //重载-运算符
    Time operator-(const Time& t) {
        if (ts < t.ts) {
            tm--;
            ts = ts + 60 - t.ts;
        } else {
            ts -= t.ts;
        }
        if (tm < t.tm) {
            th--;
            tm = tm + 60 - t.tm;
        } else {
            tm -= t.tm;
        }
        th -= t.th;
        return *this;
    }

    //格式化显示时间
    void show(const Time& t) {
        printf("%02d:%02d:%02d\n", (th + t.th) / 2, (tm + t.tm) / 2,
               (ts + t.ts) / 2);
    }
};

//字符串转整数的函数
int str_to_int(const string& s) {
    int res = 0;
    for (int i = 0; s[i]; i++) {
        res = res * 10 + (s[i] - '0');
    }
    return res;
}

string time_str1, time_str2;
int n;

void solve() {
    Time time1, time2, time_ans, time_res;
    int h = 0, d = 0, len = 0;
    scanf("%d", &n);
    //注意输入,用getline函数的注意事项
    time_str1 = "\n";
    getline(cin, time_str1);
    while (n--) {
        getline(cin, time_str1);
        //构造时间,注意题目的标准化输入
        time1 = Time(time_str1.substr(0, 8));
        time2 = Time(time_str1.substr(9, 8));
        time_ans = time2 - time1;

        len = (int)time_str1.size();
        if (len > 18) {
            h = time_ans.get_hour();
            //计算出括号里的数字,转为时间
            //注意题目比较特殊,飞行时间不超过24小时,那么括号里的数字最多为2,可以直接用str[20]-'0'计算即可
            //但是这里为了能适应更多情况,用了稍微麻烦一些的通解,当然,只是为了过题的话就不需要这么复杂
            d = str_to_int(time_str1.substr(20, len - 1 - 20));
            time_ans.set_hour(h + d * 24);
        }

        getline(cin, time_str2);
        time1 = Time(time_str2.substr(0, 8));
        time2 = Time(time_str2.substr(9, 8));
        time_res = time2 - time1;

        len = (int)time_str2.size();
        if (len > 18) {
            h = time_res.get_hour();
            d = str_to_int(time_str2.substr(20, len - 1 - 20));
            time_res.set_hour(h + d * 24);
        }
        time_ans.show(time_res);  //显示时间
    }
}

int main(void) {
    solve();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

渴望力量的猴子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值