Blue Bridge Cup Provincial Tournament Flight Time

Insert picture description hereInsert picture description here
Problem-solving ideas:

1. 读入数据,将其截取转化为时间字符串
2. 将每个时间字符串,对应的时分秒转化为整数,并存入数组
3. 如果时间字符串以(+1)结尾,则h+24,以(+2)结尾,则h+48
4. 算出两次 起飞和降落的时间差,求出其平均值,就是结果
5. 最后答案,需要将结果中 个位数前面加上零

import java.util.ArrayList;
import java.util.Scanner;

public class 航班时间 {
    
    	
		
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int N = Integer.parseInt(in.nextLine());
		ArrayList<String> results = new ArrayList<String>();
		while(N-->0) {
    
    
			String str1 = in.nextLine();
			String str2 = in.nextLine();
			String[] str0 = {
    
    str1.substring(0, 8),str1.substring(9),str2.substring(0, 8),str2.substring(9)};
//			将时间转成int数组
			int[] time1 = time_arr(str0[0]);
			int[] time2 = time_arr(str0[1]);
			int[] time3 = time_arr(str0[2]);
			int[] time4 = time_arr(str0[3]);
			//求时间差
			int[] result1 = subtime(time1,time2);
			int[] result2 = subtime(time3,time4);
			//算出两次航行时间差的平均值
			int [] result = aver_time(result1,result2);
			//对时间进行补0格式化
			String ans[] = Zero_ans(result);
			//输出
			results.add(ans[0]+":"+ans[1]+":"+ans[2]);
			
		}
		for(int i=0;i<results.size();i++) {
    
    
			System.out.println(results.get(i));
		}
	}

	  static String[] Zero_ans(int[] result) {
    
    
		  String h,m,s;
		if(result[0]<10) {
    
    
			h="0"+result[0];
		}else {
    
    
			h=""+result[0];
		}
		if(result[1]<10) {
    
    
			m="0"+result[1];
		}else {
    
    
			m=""+result[1];
		}
		if(result[2]<10) {
    
    
			s="0"+result[2];
		}else {
    
    
			s=""+result[2];
		}
		return new String[] {
    
    h,m,s};
	}

	static int[] aver_time(int[] result1, int[] result2) {
    
    
		int h,m,s;
		if((result1[0]+result2[0])%2==0) {
    
    
			h = (result1[0]+result2[0])/2;
		}else {
    
    
			result1[1]+=60;
			h=(result1[0]+result2[0])%2;
		}
		if((result1[1]+result2[1])%2==0) {
    
    
			m = (result1[1]+result2[1])/2;
		}else {
    
    
			result1[2]+=60;
			m=(result1[1]+result2[1])%2;
		}
		
		s = (result1[2]+result2[2])/2;

		return new int[] {
    
    h,m,s};
		
		
		
	}

	private static int[] subtime(int[] time1, int[] time2) {
    
    
		int h,m,s;
		if(time2[2]>=time1[2]) {
    
    
			s = time2[2]-time1[2];
		}else {
    
    
			time2[1]--;
			s = 60+time2[2]-time1[2];
		}
		if(time2[1]>=time1[1]) {
    
    
			m = time2[1]-time1[1];
		}else {
    
    
			time2[0]--;
			m = 60+time2[1]-time1[1];
		}

		h = time2[0]-time1[0];
		return new int[] {
    
    h,m,s};
	}

	static int[] time_arr(String string) {
    
    
		int h,m,s;
		if(string.contains("(+1)")) {
    
    
			h = 24+Integer.parseInt(string.substring(0, 2));
		}else if(string.contains("(+2)")) {
    
    
			h = 48+Integer.parseInt(string.substring(0, 2));
		}else {
    
    
			h = Integer.parseInt(string.substring(0, 2));
		}

		 m = Integer.parseInt(string.substring(3, 5));
		 s = Integer.parseInt(string.substring(6,8));
		return new int[] {
    
    h,m,s};
	}

}

Reference: https://blog.csdn.net/weixin_44107920/article/details/108029975

Guess you like

Origin blog.csdn.net/a12355556/article/details/115039703