话不多说,先看代码,这样总是会有数组越界的bug。。咋回事,大神来看看
public class SubMax { public static void main(String[] args) {
Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); int arr[] = new int[n]; int dp[] = new int[n]; for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
dp[i] = arr[i];
} for (int i = 1; i < n; i++) {
dp[i] = Math.max(arr[i]+dp[i-1],arr[i]);
} long max = Integer.MIN_VALUE; for (int i = 0; i < n; i++) { if (max > arr[i])
max = arr[i];
} for (int i = 0; i < n; i++) { if (max < dp[i])
max = dp[i];
}
System.out.println(max);
}
}
}