一、题目链接
AcWing 1231. 航班时间
注意本题输入的技巧:
int h1, m1, s1, h2, m2, s2, d = 0;
scanf("%d:%d:%d %d:%d:%d (+%d)", &h1, &m1, &s1, &h2, &m2, &s2, &d);
二、题目分析
(一)算法标签
模拟 数学
(二)解题思路
首先,飞机从china飞到usa,加一个时差,消耗的时间是:end1-start1+time
然后,飞机从usa飞到china,减一个时差,消耗的时间是:end2-start2-time
两式合并 end1-start1+end2-start2=2*t
t=(end1-start1+end2—start2)/2
三、AC代码
解法一:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int getTime()
{
int h1, m1, s1, h2, m2, s2, d = 0;
scanf("%d:%d:%d %d:%d:%d (+%d)", &h1, &m1, &s1, &h2, &m2, &s2, &d);
int time = (h2 + 24 * d) * 3600 + m2 * 60 + s2 - (h1 * 3600 + m1 * 60 + s1);
return time;
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i ++ )
{
int time1 = getTime();
int time2 = getTime();
int x = (time1 + time2) / 2;
int h = x / 3600 % 24, m = x / 60 % 60, s = x % 60;
printf("%02d:%02d:%02d\n", h, m, s);
}
return 0;
}