C++
$\color{gold}{— > 蓝桥杯辅导课题解}$
法一:
思路:
模拟
$\color{red}{来程:}起飞时间1 + 飞行时间 - 时差 = 降落时间1$
$\color{red}{回程:}起飞时间2 + 飞行时间 + 时差 = 降落时间2$
$求:飞行时间$
$飞行时间 = (降落时间2 - 起飞时间2 + 降落时间1 - 起飞时间1)\ / \ 2$
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int get_seconds(int h, int m, int s) {
return h * 3600 + m * 60 + s;
}
int get_time() {
string line;
getline(cin, line);
if (line.back() != ')') line += " (+0)";
int h1, m1, s1, h2, m2, s2, d;
sscanf(line.c_str(), "%d:%d:%d %d:%d:%d (+%d)", &h1, &m1, &s1, &h2, &m2, &s2, &d);
return get_seconds(h2, m2, s2) - get_seconds(h1, m1, s1) + d * 24 * 3600;
}
int main() {
int n;
cin >> n;
string line;
getline(cin, line); // 忽略第一行的回车
while (n --) {
int time = (get_time() + get_time()) / 2;
int hour = time / 3600, minute = time % 3600 / 60, second = time % 60;
printf("%02d:%02d:%02d\n", hour, minute, second);
}
return 0;
}
法二:
思路:
$与上面一样的思路,仅仅输入方法不同$
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int get_time() {
int h1, m1, s1, h2, m2, s2, d = 0; // 天数要初始化为0
scanf("%d:%d:%d %d:%d:%d (+%d)", &h1, &m1, &s1, &h2, &m2, &s2, &d); // scanf 超棒
return d * 24 * 3600 + h2 * 3600 + m2 * 60 + s2 - (h1 * 3600 + m1 * 60 + s1);
}
int main() {
int T;
cin >> T;
while(T --) {
int time = (get_time() + get_time()) / 2;
printf("%02d:%02d:%02d\n", time / 3600, time % 3600 / 60, time % 60);
}
return 0;
}