1231. Flight time

Insert picture description here
Insert picture description here
Insert picture description here
Idea: There are two difficulties in this question:
1. To find the time difference, use the formula ((end time at return-start time at return) + (end time at arrival-start time at arrival)) / 2
2. Input data Too complicated, because there is (+x) behind, the format of reading may be different, so you cannot use scanf to read, first use getline to read the data, and then add (+x) to the data without (+x) , And then use sscanf to read, c_str() will be used to return the address. For details, see the use of sscanf and the use of c_str() in the related blog

Code:

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

int get_secend(int h,int m,int s)
{
    
    
    return h * 3600 + m * 60 + s;
}

int get_time()
{
    
    
    string line;
    getline(cin,line);
    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_secend(h2,m2,s2) - get_secend(h1,m1,s1) + d * 3600 * 24;
}

int main()
{
    
    
    int t;
    cin >> t;
    string line;
    getline(cin,line);//忽略掉第一行的回车
    while(t--)
    {
    
    
        int time = (get_time() + get_time()) / 2;
        int hour = time / 3600;
        int minute = time % 3600 / 60;
        int secends = time % 60;
        printf("%02d:%02d:%02d\n",hour,minute,secends);
    }
    
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45812180/article/details/114639673