输入: 6 -2 11 -4 13 -5 -2 输出: 20
import java.util.*; public class LANQIAO1 { public static int[] a = new int[1010]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int ans = -100000000; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); ans = Math.max(ans,a[i]); }// 求最大子数列 if(ans <= 0){ System.out.println(ans); }else{ int sum = 0; for (int i = 0; i < n; i++) { // 如果sum为正数则继续保留 if(sum + a[i] > 0){ sum += a[i]; // 否则将和清零,从下一个继续 }else{ sum = 0; } // 将整个过程中最大的数保留 if(sum > ans){ ans = sum; } } System.out.println(ans); } } }