输入两组时间(时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;
}