package sumarray; import java.util.Scanner; public class MaxSum { public static int MaxSum(int array[]) { int maxsum,begin,over,sum; maxsum=0; sum=0; for(int i=0;i<array.length;i++) { if(sum<0) sum=0; sum+=array[i]; if(sum>maxsum) maxsum=sum; } System.out.print("子数组的最大值为:"+maxsum); return maxsum; } public static void main(String args[]) { int array[]=new int[10]; int maxsum=0; Scanner in=new Scanner(System.in); System.out.print("请输入一组数:"); for(int i=0;i<10;i++) { array[i]=in.nextInt(); } maxsum=MaxSum(array); System.out.print("子数组的最大值为:"+maxsum); } }
时间: 2024-10-13 17:17:24