# Flight time (question 6 of group A of 2018c / C + + of Blue Bridge Cup)

Posted by cyronuts on Sat, 05 Feb 2022 11:01:03 +0100

Title:

Title Description

Xiao h went to the United States to participate in the blue bridge cup international competition. Little h's girlfriend found that little h left at 10 a.m. and arrived in the United States at 12 a.m., so she sighed, "now the plane flies so fast that it can reach the United States in two hours.".

Little h was terrified of supersonic flight. After careful observation, it is found that the take-off and landing time of the aircraft is local time. Due to the 12 hour time difference between Beijing and the eastern United States, the plane needs a total of 14 hours of flight time.

Soon after, little h's girlfriend went to the Middle East to exchange. Xiao h doesn't know the time difference between the Middle East and Beijing. But little h got the take-off and landing time of his girlfriend's round-trip flight. Little h wants to know the flight time of his girlfriend's flight.

For a flight that may cross time zones, the take-off and landing time of the return trip is given. Assuming that the round-trip flight time of the aircraft is the same, calculate the flight time of the aircraft.

Enter description

One input contains multiple sets of data.

The first input line is a positive integer TT, indicating the number of input data groups.

Each group of data contains two lines, the first line is the take-off and landing time of the departure journey, and the second line is the take-off and landing time of the return journey.

The format of takeoff and landing time is as follows

h1:m1:s1 h2:m2:s2

or

h1:m1:s1 h3:m3:s3 (+1)

or

h1:m1:s1 h4:m4:s4 (+2)

Indicates that the flight takes off at h1:m1:s1 local time,

The first format indicates landing at h2:m2:s2 on the day of local time

The second format indicates landing at h3:00, m3:00, s3:00 the next day local time.

The third format indicates landing at h4:m4:s4 on the third day of local time.

For all questions in this topic, use H Ⓜ️ Time given in the form of S, guarantee (0 ≤ h ≤ 23,0 ≤ m,s ≤ 59)

Ensure that the input time is legal and the flight time does not exceed 24 hours.

Output description

For each group of data, one line of time hh:mm:ss is output, indicating that the flight time is HHH mm min ss.

Note that when the time is a single digit, the leading zeros should be supplemented. For example, three hours, four minutes and five seconds should be written 03:04:05.

Sample input and output

Examples

input

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)

output

04:09:05
12:10:39
14:22:05

Idea:

1. List the round-trip equation according to the meaning of the question:
If the starting point is s, the ending point is t, the flight time is t and the time difference is T1
To (A – > b): t1 - (s1 + T1) = T,
Return (B - > A): t2 - (s2 - T1) = T
Train of thought: change the time of the two places into the time of the same place by adding and subtracting the time difference at the end point, and then subtract
2. The round-trip equations are combined to obtain: (t1 - s1) + (t2-s2) = 2*T
The solution is T = [(t1 - s1) + (t2-s2)] / 2

Code 1 (not encapsulated):

```#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int num;
char a[100] = {};
(cin>>num).get();
int h1,m1,s1,h2,m2,s2,d = 0;
int firstTime,secondTime,flytime;

for(int i = 0; i < num;i++){
cin.getline(a,100);
if(strlen(a) == 17){
sscanf(a,"%d:%d:%d %d:%d:%d",&h1,&m1,&s1,&h2,&m2,&s2);
}else{
sscanf(a,"%d:%d:%d %d:%d:%d (+%d)",&h1,&m1,&s1,&h2,&m2,&s2,&d);
}
firstTime = (d * 24 + h2 - h1) * 3600 + (m2 - m1) * 60 + (s2 - s1);

cin.getline(a,100);
if(strlen(a) == 17){
sscanf(a,"%d:%d:%d %d:%d:%d",&h1,&m1,&s1,&h2,&m2,&s2);
}else{
sscanf(a,"%d:%d:%d %d:%d:%d (+%d)",&h1,&m1,&s1,&h2,&m2,&s2,&d);
}
secondTime = (d * 24 + h2 - h1) * 3600 + (m2 - m1) * 60 + (s2 - s1);
d = 0;

flytime = (firstTime + secondTime)/2;
printf("%02d:%02d:%02d\n",flytime / 3600,flytime / 60 % 60,flytime % 60 % 60);
}
return 0;
}
```

Code 2 (encapsulation):

```#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int getTime(){
char line[100] = {};
int h1,m1,s1,h2,m2,s2,t=0;
cin.getline(line,100);//getline reads the newline character after the first line 3 and generates an error, so it needs to process the newline character
if(strlen(line) == 17){
//Scan the corresponding number from the byte array according to the format
sscanf(line,"%d:%d:%d %d:%d:%d",&h1,&m1,&s1,&h2,&m2,&s2);
}else{
sscanf(line,"%d:%d:%d %d:%d:%d (+%d)",&h1,&m1,&s1,&h2,&m2,&s2,&t);
}
return (t*24 + h2-h1)*3600 + (m2 - m1) * 60 + (s2 - s1);
}
int main()
{
int t;
(cin>>t).get();//
for(int i=0;i<t;i++){
int time1 = getTime();
int time2 = getTime();
int time = (time1 + time2) / 2;
printf("%02d:%02d:%02d\n",time / 3600,time / 60 % 60,time % 60 % 60);
}
return 0;
}
```

Summary:

1. Read by line and sscanf
2. Pay attention to input and output details

Topics: C++