蓝桥杯:航班时间

【问题背景】

小h前往美国参加了蓝桥杯国际赛。小h的女朋友发现小h上午十点出发,上午十二点到达美国,于是感叹到“现在飞机飞得真快,两小时就能到美国了”。

小h对超音速飞行感到十分恐惧。仔细观察后发现飞机的起降时间都是当地时间。由于北京和美国东部有12小时时差,故飞机总共需要14小时的飞行时间。

不久后小h的女朋友去中东交换。小h并不知道中东与北京的时差。但是小h得到了女朋友来回航班的起降时间。小h想知道女朋友的航班飞行时间是多少。

【问题描述】

对于一个可能跨时区的航班,给定来回程的起降时间。假设飞机来回飞行时间相同,求飞机的飞行时间。

【输入格式】

从标准输入读入数据。

一个输入包含多组数据。

输入第一行为一个正整数T,表示输入数据组数。

每组数据包含两行,第一行为去程的 起降 时间,第二行为回程的 起降 时间。

起降时间的格式如下

h1:m1:s1 h2:m2:s2
或
h1:m1:s1 h3:m3:s3 (+1)
或
h1:m1:s1 h4:m4:s4 (+2)

表示该航班在当地时间h1时m1分s1秒起飞,

第一种格式表示在当地时间 当日 h2时m2分s2秒降落

第二种格式表示在当地时间 次日 h3时m3分s3秒降落。

第三种格式表示在当地时间 第三天 h4时m4分s4秒降落。

对于此题目中的所有以 hⓂ️s 形式给出的时间, 保证 ( 0<=h<=23, 0<=m,s<=59 ).

【输出格式】

输出到标准输出。

对于每一组数据输出一行一个时间hh:mm:ss,表示飞行时间为hh小时mm分ss秒。

注意,当时间为一位数时,要补齐前导零。如三小时四分五秒应写为03:04:05。

【样例输入】

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)

【样例输出】

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

【限制与约定】

保证输入时间合法,飞行时间不超过24小时。

资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
不要使用package语句。不要使用jdk1.7及以上版本的特性。
主类的名字必须是:Main,否则按无效代码处理。

解析

  • 不跨度一天:结束时间 - 开始时间
  • 跨度一天:23:59:60 - 开始时间+结束时间
  • 计算时间差、和
  1. 秒相减,结果为负数将flag标识为1,到分计算时减一
  2. 秒相加,结果大于60时将flag标识为1,到分计算时加1

源码


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

/**
 * <pre>
 *  Version         Date            Author          Description
 * ------------------------------------------------------------
 *  1.0.0           2020/01/15     red       -
 * </pre>
 *
 * @author red
 * @version 1.0.0 2020/1/15 1:21 PM
 * @since 1.0.0
 * 【样例输入】
 * 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)
 * <p>
 * 【样例输出】
 * 04:09:05
 * 12:10:39
 * 14:22:05
 */
public class sixth {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String N = in.nextLine();
		List<String> list = new ArrayList<>();
		for (int i = 0; i < 2 * Integer.parseInt(N); i++) {
			list.add(in.nextLine());
		}
		for (int i = 0; i < Integer.parseInt(N) * 2; i += 2) {
			String[] tempList = list.get(i).split(" ");
			String result1 = "";
			if (tempList.length == 2) {
				result1 = timeSub(tempList[0], tempList[1], null);
			} else {
				String day = tempList[2].substring(2, 3);
				result1 = timeSub(tempList[0], tempList[1], day);
			}

			String[] tempList1 = list.get(i + 1).split(" ");
			String result2;
			if (tempList1.length == 2) {
				result2 = timeSub(tempList1[0], tempList1[1], null);
			} else {
				String day = tempList1[2].substring(2, 3);
				result2 = timeSub(tempList1[0], tempList1[1], day);
			}
			System.out.println(avgTime(result1, result2));
		}
	}

	/**
	 * @param start 开始时间
	 * @param end   结束时间
	 * @param day   天数
	 * @return result
	 */
	private static String timeSub(String start, String end, String day) {
		if (day == null) {
			return subTime(start, end);
		} else {
			if (day.equals("1")) {
				String tempTime = "23:59:60";
				return addTime(subTime(start, tempTime), end);
			} else {
				String tempTime = "23:59:60";
				String temp1 = subTime(start, tempTime);
				String temp2 = addTime(temp1, tempTime);

				return addTime(temp2, end);
			}
		}
	}

	/**
	 * @param firstTime  开始时间
	 * @param secondTime 结束时间
	 * @return result
	 */
	private static String avgTime(String firstTime, String secondTime) {
		String[] firstTimeList = firstTime.split(":");
		String[] secondTimeList = secondTime.split(":");

		int flag = 0;
		StringBuilder result = new StringBuilder();
		for (int i = firstTimeList.length - 1; i >= 0; i--) {
			int temp = 0;
			if (flag > 0) {
				temp = (Integer.parseInt(firstTimeList[i]) + Integer.parseInt(secondTimeList[i])) / 2 + 1;
				flag = 0;
			} else {
				temp = (Integer.parseInt(firstTimeList[i]) + Integer.parseInt(secondTimeList[i])) / 2;
			}
			if (temp > 60) {
				flag = 1;
			}
			result.insert(0, ":" + (String.valueOf(temp).length() == 1 ? "0" + temp : temp));
		}
		return result.toString().substring(1);
	}


	/**
	 * @param start 开始时间
	 * @param end   结束时间
	 * @return result
	 */
	private static String subTime(String start, String end) {
		String[] startList = start.split(":");
		String[] endList = end.split(":");

		StringBuilder result = new StringBuilder();
		int flag = 0;
		for (int i = startList.length - 1; i >= 0; i--) {
			int temp = 0;
			if (flag > 0) {
				temp = (Integer.parseInt(endList[i]) - Integer.parseInt(startList[i])) - 1;
				flag = 0;
			} else {
				temp = (Integer.parseInt(endList[i]) - Integer.parseInt(startList[i]));
				if (temp < 0) {
					temp += 60;
					flag = 1;
				}
			}
			result.insert(0, ":" + (String.valueOf(temp).length() == 1 ? "0" + temp : temp));
		}
		return result.toString().substring(1);
	}

	/**
	 * @param start 开始时间
	 * @param end   结束时间
	 * @return result
	 */
	private static String addTime(String start, String end) {
		String[] startList = start.split(":");
		String[] endList = end.split(":");

		StringBuilder result = new StringBuilder();
		int flag = 0;
		for (int i = startList.length - 1; i >= 0; i--) {
			int temp = 0;
			if (flag > 0) {
				temp = (Integer.parseInt(endList[i]) + Integer.parseInt(startList[i])) + 1;
				if (temp > 60) {
					temp -= 60;
					flag = 1;
				} else {
					flag = 0;
				}
			} else {
				temp = (Integer.parseInt(endList[i]) + Integer.parseInt(startList[i]));
				if (temp > 60) {
					temp -= 60;
					flag = 1;
				}
			}
			result.insert(0, ":" + (String.valueOf(temp).length() == 1 ? "0" + temp : temp));
		}
		return result.toString().substring(1);
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# OOP(机试) 本程序总结文章:http://blog.qiji.tech/?p=10344 - - - ## 程序基本要求 一、项目名称: Air Infomation Programming 基于控制台的航班信息程序,简称AIP 二、具体要求如下: (1)显示航班信息程序主菜单,如图-1所示,包括: * 1)列出所有航班 * 2)按起飞时间查询 * 3)按目的地查询 * 4)删除航班 * 5)更新航班 * 6)退出系统 (2)列出所有航班:查出所有航班的信息,以列表形式显示,包括:编号,航班号,目的地,起飞日期。 (3)按起飞时间查询:输入起飞时间(格式如2011-2-25),查出所有这一天的航班。 (4)按目的地查询:输入目的地,查出所有飞往此地的航班。 (5)删除航班:删除指定编号的航班。 (6)更新航班:更新指定编号的航班。 (7)退出系统。 三、类的设计 需要定义如下类 * 航班信息实体类(AirInfo) * 航班编号(id) * 航班号(flight_number) * 目的地(destination) * 起飞日期(flight_date) * 航班信息管理类AirInfoManager类 * 程序入口类TestAirInfo类 四、具体要求及推荐实现步骤 1. 创建实体类AirInfo,属性私有化,根据业务提供需要的构造方法和setter/getter方法。 1. 创建航班管理AirInfoManager类,在类中提供列出所有航班的方法,按起飞时间查询的方法、按目的地查询的方法、删除航班的方法、更新航班的方法、退出程序的方法。 2. 创建TestAirInfo类,启动和运行程序。 3. 航班的信息用ArrayList(或数组)保存。 4. 要求代码规范,命名正确。 - - -

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值