题目https://www.luogu.com.cn/problem/P8665
输入处理:
sc.nextInt()
读取测试用例数量t
。sc.nextLine()
读取并丢弃剩余的换行符,避免影响后续输入。
时间解析:
parse(String s)
方法解析每行输入,分割起飞时间、降落时间和跨天信息。使用
replaceAll("[^0-9]", "")
提取跨天天数,转换为整数days
。
时间转换:
parseTime(String st, int d)
将时间字符串转换为秒数,加上跨天的秒数。计算起飞时间和降落时间的差值,得到单程飞行时间。
格式化输出:
format(int d)
将总秒数转换为hh:mm:ss
格式,使用String.format
确保前导零。
import java.util.Scanner;
public class Main {
static int N = 200010;
public static void main(String[] args) {
int[] a = new int[N];
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
while(t-- > 0) {
String s1 = sc.nextLine().trim();
String s2 = sc.nextLine().trim();
int d1 = parse(s1);
int d2 = parse(s2);
int d = (d1 + d2)/2;
System.out.println(format(d));
}
}
private static String format(int d) {
int h = d/3600;
int m = d%3600/60;
int s = d%3600%60;
return String.format("%02d:%02d:%02d", h,m,s);
}
private static int parse(String s) {
String[] h = s.split("\\s+");
String st = h[0];
String ed = h[1];
int d = 0;
if(h.length >= 3) {
String day = h[2];
d = Integer.parseInt(day.replaceAll("[^0-9]", ""));
}
int begin = parseTime(st,0);
int end = parseTime(ed,d);
return end-begin;
}
private static int parseTime(String st, int d) {
String[] t = st.split(":");
int h = Integer.parseInt(t[0]);
int m = Integer.parseInt(t[1]);
int s = Integer.parseInt(t[2]);
return d*86400+h*3600+m*60+s;
}
}
评论