跳到正文

题解

[Java 练习] ECNUOJ 3591. GPS 数据处理

jyttoby约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年1月17日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 2 分钟

版本

  1. v1当前

题目链接

善用 .indexOf() 和题目中提到的 Integer.parseInt()。

代码(198ms/28.105MB,在 Java 提交中最快):

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));
  }
}
原文