第二题: public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int maxDistance = 0; int currentPosition = 0; for (int i = 0; i < n; i++) { int a = in.nextInt(); // 不反转情况下的新位置 int noReserve = currentPosition + a; // 更新最大距离 maxDistance = Math.max(maxDistance, Math.abs(noReserve)); // 计算反转后的新位置 int reserve = -currentPosition + a; // 更新最大距离 maxDistance = Math.max(maxDistance, Math.abs(reserve)); // 更新当前位置 currentPosition = noReserve; } System.out.println(maxDistance); }