C++ flight time (time conversion, simulation, sscanf input control)

Xiao h went to the United States to participate in the Blue Bridge Cup International.
Xiao H’s girlfriend discovered that Xiao H departed at 10 am and arrived in the United States at 12 in the morning, so she sighed that “the plane is flying so fast now, and it will take two hours to reach the United States”. Xiao H is very terrified of supersonic flight. After careful observation, it was found that the take-off and landing times of the aircraft were all local time. As there is a 12-hour time difference between Beijing and the eastern United States, the aircraft needs a total of 14 hours of flight time. Soon H's girlfriend went to the Middle East to exchange. Little h does not know the time difference between the Middle East and Beijing. But Xiao H got his girlfriend's return flight time. Little h wants to know the flight time of his girlfriend's flight. For a flight that may cross time zones, given the round-trip take-off and landing time. Assuming that the flight time of the plane is the same, find the flight time of the plane.
Input format
One input contains multiple sets of data. The first line of input is a positive integer T, which represents the number of input data groups.
Each set of data contains two lines, the first line is the take-off and landing time of the outbound journey, and the second line is the take-off and landing time of the return journey.
Takeoff and landing the following format:
h1 of: M1: S1 H2: M2: S2
h1 of: M1: S1 H3: M3: S3 (+ 1'd)
h1 of: M1: S1 H4: M4: S4 (+2)
a first format The flight took off at h1: m1: s1, local time, and landed at h2: m2: s2, local time.
The second format means that the flight takes off at h1: m1: s1, local time, and landed at h2: m2: s2, the next day, local time.
The third format means that the flight takes off at h1: m1: s1, local time, and landed at h2: m2: s2, local time on the third day.
Output format
For each group of data, output one line and one time hh:mm:ss, which means the flight time is hh hours mm minutes ss seconds.
Note that when the time is a single digit, the leading zeros should be filled in. For example, three hours, four minutes and five seconds should be written as 03:04:05.
data range
Ensure that the input time is legal (0≤h≤23, 0≤m, s≤59) and the flight time does not exceed 24 hours.
Input example:
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 example:
04:09:05
12:10:39
14:22:05

AC code:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

int n;

int get_second(int hour,int minute,int second)
{
    
    
    return 3600*hour+60*minute+second;
}

int get_time()
{
    
    
    string line;
    getline(cin,line);
    //back可以得到最后一个字符;弄成统一格式方便之后处理
    if(line.back()!=')') 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*3600*24;
}

int main()
{
    
    
    scanf("%d",&n);
    string line;
    getline(cin,line);
    while(n--)
    {
    
    
        //往返的时差已相互抵消,相加÷2得到单程总秒数
        int time=(get_time()+get_time())/2;
        //时分秒转换
        int hour=time/3600,minute=(time%3600)/60,second=time%60;
        //不满2位就补0
        printf("%02d:%02d:%02d\n",hour,minute,second);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_44643644/article/details/108891226