第六题
问题描述:
小h前往美国参加了蓝桥杯国际赛。小h的女朋友发现小h上午十点出发,上午十二点到达美国,于是感叹到“现在飞机飞得真快,两小时就能到美国了”。
小h对超音速飞行感到十分恐惧。仔细观察后发现飞机的起降时间都是当地时间。由于北京和美国东部有12小时时差,故飞机总共需要14小时的飞行时间。
不久后小h的女朋友去中东交换。小h并不知道中东与北京的时差。但是小h得到了女朋友来回航班的起降时间。小h想知道女朋友的航班飞行时间是多少。
对于一个可能跨时区的航班,给定来回程的起降时间。假设飞机来回飞行时间相同,求飞机的飞行时间。
输入:
一个输入包含多组数据。
输入第一行为一个正整数T,表示输入数据组数。
每组数据包含两行,第一行为去程的 起降 时间,第二行为回程的 起降 时间。
起降时间的格式如下
h1:m1:s1 h2:m2:s2
h1:m1:s1 h3:m3:s3 (+1)
h1:m1:s1 h4:m4:s4 (+2)
表示该航班在当地时间h1时m1分s1秒起飞,
第一种格式表示在当地时间 当日 h2时m2分s2秒降落
第二种格式表示在当地时间 次日 h3时m3分s3秒降落。
第三种格式表示在当地时间 第三天 h4时m4分s4秒降落。
对于此题目中的所有以 h : m : s 形式给出的时间, 保证 ( 0<=h<=23, 0<=m,s<=59 ).
保证输入时间合法,飞行时间不超过24小时。
输出:
对于每一组数据输出一行一个时间hh:mm:ss,表示飞行时间为hh小时mm分ss秒。
注意,当时间为一位数时,要补齐前导零。如三小时四分五秒应写为03:04:05。
样例输入:
3
17:48:19 21:57:24
11:05:18 15:14:23
17:21:07 00:31:46 (+1)
23:02:41 16:13:20 (+1)
10:19:19 20:41:24
22:19:04 16:41:09 (+1)
样例输出:
04:09:05
12:10:39
14:22:05
思路:
①
当地去程出发时间 + 时差 + 实际耗时 = 当地去程到达时间
当地回程出发时间 - 时差 + 实际耗时 = 当地回程到达时间
② 为了减少时间数字的运算,把所有时间统一换算成秒,如果有(+1)或者(+2)就加上3600×24秒或者3600×48秒
③ 读入字符串用cin.getline()比较方便,可以忽略空格,但是输入n后记得要getchar()吞掉回车
④ 可能是最废的代码
# include <iostream>
# include <stdio.h>
# include <stdlib.h>
# include <string>
using namespace std;
int main()
{
int n;
cin>>n;
getchar();
char h[24],t[24];
string hstr,tstr;//存储字符串
int hs,ts;//字符串长度
int fh,ft = 0;//是否是隔天,隔了几天
int qsh,qsm,qss;//去程起始时间 小时 分钟 秒
int qeh,qem,qes;//去程结束时间 小时 分钟 秒
int hsh,hsm,hss;//回程起始时间 小时 分钟 秒
int heh,hem,hes;//回程结束时间 小时 分钟 秒
int qu_start_time,qu_end_time;
int hui_start_time,hui_end_time;
int start,end,gap;
int out_h,out_m,out_s;
while(n){
cin.getline(h,24);
cin.getline(t,24);
n--;
hstr = h;tstr = t;
hs = hstr.size();ts = tstr.size();
if(hs==22)
fh = hstr[20]-48;
else
fh = 0;
if(ts==22)
ft = tstr[20]-48;
else
fh = 0;
qsh = (hstr[0]-48)*10+(hstr[1]-48);
qsm = (hstr[3]-48)*10+(hstr[4]-48);
qss = (hstr[6]-48)*10+(hstr[7]-48);
qu_start_time = qsh*3600+qsm*60+qss;
qeh = (hstr[9]-48)*10+(hstr[10]-48);
qem = (hstr[12]-48)*10+(hstr[13]-48);
qes = (hstr[15]-48)*10+(hstr[16]-48);
qu_end_time = (qeh+fh*24)*3600+qem*60+qes;
hsh = (tstr[0]-48)*10+(tstr[1]-48);
hsm = (tstr[3]-48)*10+(tstr[4]-48);
hss = (tstr[6]-48)*10+(tstr[7]-48);
hui_start_time = hsh*3600+hsm*60+hss;
heh = (tstr[9]-48)*10+(tstr[10]-48);
hem = (tstr[12]-48)*10+(tstr[13]-48);
hes = (tstr[15]-48)*10+(tstr[16]-48);
hui_end_time = (heh+ft*24)*3600+hem*60+hes;
start = qu_start_time + hui_start_time;
end = qu_end_time + hui_end_time;
gap = (end-start)/2;
out_h = gap/3600;
gap -= (out_h)*3600;
out_m = gap/60;
gap -= (out_m)*60;
out_s = gap;
if(out_h<10)
cout<<"0";
cout<<out_h<<":";
if(out_m<10)
cout<<"0";
cout<<out_m<<":";
if(out_s<10)
cout<<"0";
cout<<out_s<<endl;
}
return 0;
}