题目
小 h 前往美国参加了蓝桥杯国际赛。小 h 的女朋友发现小 h 上午十点出发,上午十二点到达美国,于是感叹到“现在飞机飞得真快,两小时就能到美国了”。
小 h 对超音速飞行感到十分恐惧。仔细观察后发现飞机的起降时间都是当地时间。由于北京和美国东部有 12 小时时差,故飞机总共需要 14 小时的飞行时间。
不久后小 h 的女朋友去中东交换。小 h 并不知道中东与北京的时差。但是小 h 得到了女朋友来回航班的起降时间。小 h 想知道女朋友的航班飞行时间是多少。
对于一个可能跨时区的航班,给定来回程的起降时间。假设飞机来回飞行时间相同,求飞机的飞行时间。
分析
本来以为一趟飞机的起降时间不在一个时区,只能时区相同的时间进行加减,但是后来发现是在同一个时区,只需要将来回航班的时间算出相加/2即可,但是题目说假设来回时间相同,但是样例实际是不同的,这里存疑,可能是我题目理解存在问题。为了方便计算,先将所有时间换算成秒,然后再进行计算。本题的难点在于,读入时间不好处理,可先将时间读入,再判断后续是否有字符,如果后续没有字符说明是当天来回,可直接减,有字符需要在降落时间点加上多出的几天时间。
代码
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define guanliu ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const ll maxn=1e5+7;
const ll mod=1e9+7;
const ll INF=0x3f3f3f3f;
const double pi=acos(-1);
int gettime()
{
int h1,h2,m1,m2,s1,s2;
scanf("%d:%d:%d %d:%d:%d",&h1,&m1,&s1,&h2,&m2,&s2);
char x;
int f=0;
while((x=getchar())!='\n')
{
if(isdigit(x))
{
f=x-'0';
}
}
int time=0;
if(f==0) time=h2*3600+m2*60+s2-h1*3600-m1*60-s1;
else time=f*24*3600+h2*3600+m2*60+s2-h1*3600-m1*60-s1;
return time;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int x=gettime();
int y=gettime();
int ans=(x+y)/2;
int h=ans/3600;
h%=24;
and%=3600;
int m=ans/60;
ans%=60;
printf("%02d:%02d:%02d\n",h,m,ans);
}
return 0;
}