善用 .indexOf() 和题目中提到的 Integer.parseInt()。
代码(198ms/28.105MB,在 Java 提交中最快):
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str, ans = "";
do {
str = in.nextLine();
if (str.length() <= 7) {
continue;
}
if (str.substring(0, 7).equals("$GPRMC,") &&
str.charAt(str.indexOf(',', str.indexOf(',') + 1) + 1) == 'A') {
char val = str.charAt(1);
int len = str.length() - 3;
for (int i = 2; i < len; ++i) {
val = (char)(val ^ str.charAt(i));
}
if (Integer.parseInt(str.substring(str.length() - 2), 16)
== val % 65536) {
ans = str.substring(7, 13);
}
}
} while (!str.equals("END"));
int h = (Integer.parseInt(ans.substring(0, 2)) + 8) % 24;
if (h < 10) {
System.out.print("0");
}
System.out.println(h + ":" + ans.substring(2, 4) + ":" + ans.substring(4));
}
}