import java.util.Scanner; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.println("请输入数组的长度:"); int length=in.nextInt(); System.out.println("请输入一个数组:"); int[] a=new int [length]; for(int i=0;i<length;i++) { a[i]=in.nextInt(); } int max=a[0]; int sum=0; for(int m=0;m<length;m++) { if(sum<=0) { sum=a[m]; } else { sum+=a[m]; } if(sum>max) { max=sum; } } System.out.println("子数组最大的和为"+max); } }
时间: 2024-10-09 05:06:42