Design idea: On the basis of the original largest array, the array is expanded first to twice times the original array is copied and then connected to the back. Then enter a number of times the loop, each cycle after the beginning of the scan to enter a number of numbers. Computes the array and continuously updates the maximum number combination
Program Source code:
Packagetest;ImportJava.util.Scanner; Public classTest { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); System.out.print ("Please enter the array length:"); intL=in.nextint ();//user Input Array length int[] Shuzu =New int[L*2]; intNumber_hezuidazhi=0; System.out.println ("Please enter the number in turn"); for(inti=0;i<l;i++) {Shuzu[i]=In.nextint (); Shuzu[i+l]=Shuzu[i]; } intNumber_he=shuzu[0]; for(intm=0;m<l;m++)//Enter a number of times to loop { for(intn=m;n<m+l;n++)//Number of digits to be scanned backwards each time{number_he= Number_he +Shuzu[n]; if(number_hezuidazhi<number_he)//if the array and is greater than the maximum array, the update{Number_hezuidazhi=number_he; } if(number_he<0)//if the array and is less than 0, then discard starts from zero{number_he=0; }} number_he= 0;//updates the array one time and} System.out.println ("Maximum array and for" +Number_hezuidazhi); In.close (); }}
Results:
Result Analysis: Input-1 2 3-4 6
So in this end-to-end array, it should be discarded-4 of all the other numbers make up the largest set.
The program changes the array to-1 2 3-4 6-1 2 3-4 6
First Scan (-1 2 3-4 6)-1 2 3-4 6
Second Scan-1 (2 3-4 6-1) 2 3-4 6
。。。。
Is the algorithm of the last largest array each time
Summary: The time complexity of the program becomes O (n*n); there is no way to implement it once, and the array changes are traversed multiple times.
Returns the maximum number of sub-arrays in a one-dimensional integer array