Blue bridge cup-2018-flight time-c + + / Java / Python 3

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 get to 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 takeoff 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 takeoff 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 TTT, 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:m3:s3 on the next day local time.

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

For all times given in the form of h:m:s in this topic, guarantee (0 ≤ h ≤ 23,0 ≤ m,s ≤ 590 \leq h \leq 23, 0 \leq m,s \leq 590 ≤ 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, output one line of time hh:mm:ss, 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

Example

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

Operating limits

  • Maximum running time: 1s
  • Maximum operating memory: 256M

 

Solution 1: Python
 

import os
import sys

def counttime(time1,time2):
     tt=0
     for i in [time1,time2]:
          t=[]
          h=0
          m=0
          s=0
          h1=int(i[0][0:2])
          m1=int(i[0][3:5])
          s1=int(i[0][6:8])
          
          h2=int(i[1][0:2])
          m2=int(i[1][3:5])
          s2=int(i[1][6:8])
          
          tnum=h1*3600+m1*60+s1
          t.append(tnum)
          if(len(i)==3):
               day=int(i[2][2])
               tnum=day*24*3600+h2*3600+m2*60+s2
               t.append(tnum)
          else:
               tnum=h2*3600+m2*60+s2
               t.append(tnum)
          st=t[1]-t[0]
          tt=tt+st
     tt=int(tt/2)
     hh=int(tt/3600)
     tt=tt%3600
     mm=int(tt/60)
     ss=tt%60
     return hh,mm,ss
 
if __name__=="__main__":
     num=int(input())
     a=[]
     while num:
          t1=str(input()).split(' ')
          t2=str(input()).split(' ')
          hh,mm,ss=counttime(t1,t2)
          print("{:0>2d}:{:0>2d}:{:0>2d}".format(hh,mm,ss))
          num=num-1

Solution 2: c++
 

#include <bits/stdc++.h>
using namespace std;
const int day = 24*60*60;
const int hour = 60*60;
const int minute = 60;//All in seconds
int input1(void)
{
    int a,b,c;
    scanf("%d:%d:%d",&a,&b,&c);
    int  Time = a*hour+b*minute+c;
    return Time;
}
int input2(void)
{
    int a,b,c;
    scanf("%d:%d:%d",&a,&b,&c);
    int  Time = a*hour+b*minute+c;
    char ch;
    while((ch = getchar()) !='\n'&&ch != '\r')
    {
        if(ch == '(')
        {
            getchar();
            ch = getchar();
            Time += (ch-'0')*day;
        }
    }
    return Time;
}
void print(int Time)//The problem has been guaranteed within 24 hours
{
    int a,b,c;
    a = Time/hour,Time %= hour;
    b = Time/minute,Time %= minute;
    c = Time;
    printf("%02d:%02d:%02d\n",a,b,c);
}
int main(void)
{
    int n;
    cin>>n;
    while(n--)
    {
        int start1 = input1();
       // print(start1);
        int end1 = input2();
        //print(end1);
        int start2 = input1();
        int end2 = input2();
        int ans = 0;
        ans += end1-start1;
        ans += end2-start2;
        ans /= 2;
        print(ans);
    }
    return 0;
}

Solution 3: Java
 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
 
public class T {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws ParseException {
        int n = sc.nextInt();
        sc.nextLine();
        for(int i=0;i<n;i++) {
            long t1 = getTime();//Time difference between the first a - > b
            long t2 = getTime();//Time difference between round trip B - > A
            long t = (t1+t2)/2; //Add the time difference between two times / 2
            System.out.printf("%02d:%02d:%02d\n",t/3600,t/60%60,t%60);//Output hours, minutes and seconds.% There are two digits in the 2d table. If it is insufficient, make up 0
        }
    }
    private static long getTime() throws ParseException {
        String line = sc.nextLine();//Enter takeoff time arrival time
        String[] split = line.split(" "); //Get an array of departure time, arrival time and interval days
        SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss");//Date formatted display (10:02:03)
        Date t1 = sd.parse(split[0]); //Convert a date of string type to a date in the above format
        Date t2 = sd.parse(split[1]);
        
        int day=0;//Record the number of days spanned
        if(split.length==3) {//Span of days
            day = Integer.parseInt(split[2].substring(2, 3));//Including 2 but excluding 3 (both 2 and 3 indicate subscripts, which start from 0)
        }
        return day*24*60*60 + t2.getTime()/1000 - t1.getTime()/1000;//Returns the total number of seconds between round trips. The getTime method returns long data in milliseconds.
    }
}

Keywords: Python Java C++

Added by Galahad on Sun, 23 Jan 2022 19:10:48 +0200