作业总结-后续
考虑时间复杂度的话
思路就是知道前i-1个最大和,判断第i个
import java.util.Scanner;
public class other_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入数组长度");
int length = scanner.nextInt();
int tempInt[] = new int[length];
int max = 0;
int tempMax = 0;//当前最大值
for (int i = 0; i < length; i++) {
tempInt[i] = scanner.nextInt();//一个元素一个元素的输入
if (i == 0) {//第一个是元素
tempMax = tempInt[i];
max = tempInt[i];
} else {
if (tempMax < 0) {
tempMax = tempInt[i];
} else {
tempMax += tempInt[i];
}
}
if (tempMax > max) {
max = tempMax;
}
}
System.out.println(max);
}
}