题目
标题:航班时间
【问题背景】
小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 ).
【输出格式】
输出到标准输出。
对于每一组数据输出一行一个时间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
【限制与约定】
保证输入时间合法,飞行时间不超过24小时。
资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
注意:
main函数需要返回0;
只使用ANSI C/ANSI C++ 标准;
不要调用依赖于编译环境或操作系统的特殊函数。
所有依赖的函数必须明确地在源文件中 #include <xxx>
不能通过工程设置而省略常用头文件。
提交程序时,注意选择所期望的语言类型和编译器类型。
代码
1 #include<iostream>
2 #include<cstdio>
3 using namespace std;
4 char str[50];
5 //初始化字符串数组
6 void init(){
7 for(int i=0;i<35;i++){
8 str[i]='0';
9 }
10 }
11 //string to int
12 int strTint(char h1,char h2,char m1,char m2,char s1,char s2){
13 cout<<h1<<h2<<" "<<s1<<s2<<endl;
14 int hts=((h1-'0')*10+(h2-'0'))*3600;
15 int mts=((m1-'0')*10+(m2-'0'))*60;
16 int sec=hts+mts+((s1-'0')*10+(s2-'0'));
17 cout<<hts<<" "<<mts<<" "<<sec<<endl;
18 return sec;
19 }
20 //int to string & display
21 int intTstr(int t){
22 int h=t/3600;
23 int m=(t-h*3600)/60;
24 int s=t-h*3600-m*60;
25 switch(((h>=10)?1:0)){
26 case 0:cout<<"0"<<h<<":";
27 break;
28 default: cout<<h<<":";
29 break;
30 }
31 switch(((m>=10)?1:0)){
32 case 0:cout<<"0"<<m<<":";
33 break;
34 default: cout<<m<<":";
35 break;
36 }
37 switch(((s>=10)?1:0)){
38 case 0:cout<<"0"<<s<<endl;
39 break;
40 default: cout<<s<<endl;
41 break;
42 }
43 }
44 int main(){
45 int t;
46 cin>>t;
47 getchar();//读取一个字符串,消除"\n"的影响
48 int t1s,t1e,t1,t2s,t2e,t2,final;
49 while(t--){
50 init();
51 gets(str);//获取一行字符串
52 t1s=strTint(str[0],str[1],str[3],str[4],str[6],str[7]);
53 t1e=strTint(str[9],str[10],str[12],str[13],str[15],str[16]);
54 if(str[19]=='+'){
55 t1e+=(str[20]-'0')*(3600*24);
56 }
57 t1=t1e-t1s;
58 cout<<t1<<endl;
59 init();
60 gets(str);
61 t2s=strTint(str[0],str[1],str[3],str[4],str[6],str[7]);
62 t2e=strTint(str[9],str[10],str[12],str[13],str[15],str[16]);
63 if(str[19]=='+'){
64 t2e+=(str[20]-'0')*(3600*24);
65 }
66 t2=t2e-t2s;
67 cout<<t2<<endl;
68 final=(t2+t1)/2;
69 cout<<final<<endl;
70 intTstr(final);
71 }
72 }
来源:https://www.cnblogs.com/memocean/p/12292363.html