航班时间
【问题背景】
小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: m: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,否则按无效代码处理。
java代码1
大体思路是处理完输入以后,将每个航班的时间转化为以秒为单位,将降落时间减去出发时间即为航班飞行时间。
从A到B地的时差与从B到A的时差抵消了,所以只需要将两个出发到达时间之差取平均数即可。
import java.util.Scanner;
public class Main {
static String[] strs;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
strs = new String[4 * N];
for (int i = 0; i < 4 * N; i++) {
strs[i] = new String(sc.next());
// 如果输入的时间加了括号,就需要把该字符串附加到前一个字符串,注意题目给出的数据中左括号前是有空格的
if (strs[i].charAt(0) == '(') {
strs[i - 1] = strs[i - 1] + strs[i];
i--;
}
}
//注意这里不要漏掉最后一个括号中的数
if (sc.hasNext()) {
String str = new String(sc.next());
strs[4 * N - 1] = strs[4 * N - 1] + str;
}
// 一次读入两次出发、到达时间
for (int i = 0; i < N; i++) {
// 将所有的时间都转化为s为单位
long time1 = toLong(strs[i * 4]);
long time2 = toLong(strs[i * 4 + 1]);
long time3 = toLong(strs[i * 4 + 2]);
long time4 = toLong(strs[i * 4 + 3]);
// long time1 = toLong("10:19:19");
// long time2 = toLong("20:41:24");
// long time3 = toLong("22:19:04");
// long time4 = toLong("16:41:09(+1)");
long time = (time2 + time4 - time1 - time3) / 2;
// 将差值再转化为时分秒的格式
long h, m, s;
h = time / 3600;
time %= 3600;
m = time / 60;
time %= 60;
s = time;
// 输出
print(h, m, s);
}
sc.close();
}
private static void print(long h, long m, long s) {
if (h < 10) {
System.out.print("0");
}
System.out.print(h + ":");
if (m < 10) {
System.out.print("0");
}
System.out.print(m + ":");
if (s < 10) {
System.out.print("0");
}
System.out.println(s);
}
private static long toLong(String str) {
int h;
int m;
int s;
long res = 0;
char c;
h = (str.charAt(0) - '0') * 10 + str.charAt(1) - '0';
m = (str.charAt(3) - '0') * 10 + str.charAt(4) - '0';
s = (str.charAt(6) - '0') * 10 + str.charAt(7) - '0';
res += h * 3600 + m * 60 + s;
if (str.length() > 8) {
int t = str.charAt(10) - '0';
res += t * 24 * 3600;
}
return res;
}
}
java代码2
上个代码中获取输入以及输出操作着实笨比,这里给出另一种解法,利用了很多java日期的API,不了解的朋友可以去看我一篇关于日期的博客。
import java.text.*;
import java.util.*;
public class Main {
private static Scanner sc;
private static long[] res;
private static int N;
public static void main(String args[]) {
sc = new Scanner(System.in);
N = sc.nextInt();
res = new long[N];
sc.nextLine();// 这行代码一定不能省略,因为nextInt之后有回车,该语句用来接受回车,否则之后的nextLine不能得到预期结果
for (int i = 0; i < N; i++) {
long time1 = 0, time2 = 0;
try {
time1 = getTime();
time2 = getTime();
} catch (ParseException e) {
e.printStackTrace();
}
res[i] = (time1 + time2) / 2;
}
for (int i = 0; i < N; i++) {
System.out.printf("%02d:%02d:%02d\n", res[i] / 3600, res[i] % 3600 / 60, res[i] % 3600 % 60);
}
sc.close();
}
// 获取一行输入,分别得到航班出发时间和返回时间,将时间转化为数字,最后返回他们的差
private static long getTime() throws ParseException {
long res = 0;
String str = sc.nextLine();
String[] lines = str.split(" ");
SimpleDateFormat ft = new SimpleDateFormat("hh:mm:ss");
// 将字符串解析为Date类型
Date time1 = ft.parse(lines[0]);
Date time2 = ft.parse(lines[1]);
if (lines.length == 3) {
long day = lines[2].charAt(2) - '0';
res += day * 24 * 3600;
}
res += time2.getTime() / 1000 - time1.getTime() / 1000;
return res;
}
}