格式要求:
题解:深刻体会到scanf和printf在格式化输入输出的方便
#include<iostream>
#include <cstdio>
#include<cstring>
using namespace std;
//把时间换算成秒
int get_second(int h, int m, int s)
{
return h * 3600 + m * 60 + s;
}
//读入一行时间数据
int get_time(){
string line;
//不能使用cin读入 因为遇空格停止
getline(cin,line);
if(line.back()!=')'){
//在没有(+1)的数据后面添上(+0)
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_second(h2, m2, s2) - get_second(h1, m1, s1) + d * 24 * 3600;
}
int main(){
/*
假设出发时间为S,到达时间为E,时差为T,飞行时间为X,则列式:
S1+X+T=E1;E2+T-S2 =X;整理得:X=(E1-S1)/2+(E2-S2)/2;
式子得出,主要是输入输出操作
*/
int n = 0;
cin>>n;
//读取一个换行到时间数据行
string line;
getline(cin, line);
while(n--){
int t = (get_time()+get_time())/2;
//转换成时分秒
int hour = t/3600;
//得到除去小时数的剩余秒
int minute = t%3600/60;
int second = t%60;
printf("%02d:%02d:%02d\n", hour, minute, second);
}
return 0;
}