课堂练习:
练习求一个数组最大连续和,
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int[] list1 = new int[1000000];
int n = cin.nextInt();
for (int i = 0; i < 10; i++) {
list1[i] = cin.nextInt();
}
int[] list2 = new int[1000000];
int[] list3 = new int[1000000];
list2[0] = list1[0];
for (int i = 1; i < n; i++) {
list2[i] = list2[i - 1] + list1[i];
}
int m = cin.nextInt();
for (int i = 0; i < n - m; i++) {
if (i == 0) list3[i] = list2[m - 1];
list3[i] = list2[i + m - 1] - list2[i];
}
int max = list3[0];
for (int i = 0; i < list3.length; i++) {
if (list3[i] > max) max = list3[i];
}
System.out.println(max);
}
}