平均时间

输入两组时间(时h,分m, 秒s),计算平均时间,两组时间差不超过1小时,h在0-11之间。

输入样例

1 20 30 1 30 30

0 20 30 11 20 30 

输出样例 

1 25 30

11 55 30

#include <iostream>

using namespace std;

//比较两个时间的大小,若1大则返回true,否则返回false
bool compareTime(int h1, int m1, int s1, int h2, int m2, int s2) {
    if (h1 > h2) {
        if (h1 == 11 && h2 == 0) {
            return false;
        }else {
            return true;
        }
    } else if (h1 < h2) {
        if (h1 == 0 && h2 == 11) {
            return true;
        } else {
            return false;
        }
    } else {
        if (m1 > m2) {
            return true;
        } else if (m1 < m2) {
            return false;
        } else {
            return s1 >= s2 ? true : false;
        }
    }
}

void swapTime(int &h1, int &m1, int &s1, int &h2, int &m2, int &s2) {
    int temp = h1;
    h1 = h2;
    h2 = temp;
    temp = m1;
    m1 = m2;
    m2 = temp;
    temp = s1;
    s1 = s2;
    s2 = temp;
}

int difference(int h1, int m1, int s1, int h2, int m2, int s2) {
    int res = 0;
    if (s1 <= s2) {
        for (int i = s1; i < s2; i++) {
            res += 1;
        }
    } else {
        //10 30 30   10 40 20
        for (int i = s2; i < s1; i++) {
            res -= 1;
        }
    }
    if (m1 <= m2) {
        for (int i = m1; i < m2; i++) {
            res += 60;
        }
    } else {
        for (int i = m2; i < m1; i++) {
            res -= 60;
        }
    }
    if (m2 < m1) {
        res += 3600;
    }
    if (m1 == m2 && s1 == s2 && h1 != h2) {
        res = 3600;
    }
    return res;
}

void add(int h1, int m1, int s1, int addTime) {
    int res_h, res_m, res_s;
    int addMinute = addTime/60;
    int addSecond = addTime%60;
    res_s = (s1+addSecond)%60;

    if (s1+addSecond > 60) {
        m1 += 1;
    }

    res_m = (m1+addMinute)%60;


    if (m1+addMinute >= 60) {
        h1 += 1;
    }

    res_h = h1%12;
    cout << res_h << " " << res_m << " " << res_s << endl;
}

int main()
{
    int h1, m1, s1, h2, m2, s2;
    for (int i = 0; i < 2; i++) {
        cin >> h1 >> m1 >> s1 >> h2 >> m2 >> s2;

        //让时间2比时间1大
        if (compareTime(h1, m1, s1, h2, m2, s2)) {
            swapTime(h1, m1, s1, h2, m2, s2);
        }

        int diff = difference(h1, m1, s1, h2, m2, s2);
        int addTime = diff/2;
        add(h1, m1, s1, addTime);

    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值