PackageZuoye;ImportJava.util.Scanner; Public classMaxsubarray { Public Static voidMain (string[] args) {Scanner scan=NewScanner (system.in); //input DataSystem.out.println ("Please enter array length"); intn =Scan.nextint (); int[] A =New int[n]; System.out.println ("Please enter array elements"); for(inti = 0;i < n;i++) {A[i]=Scan.nextint (); } scan.close (); //computes the and largest contiguous subarray of this array int[] result =maxsub (a,a.length); System.out.println ("Do not concatenate the ring and the largest contiguous subarray:"); for(inti = Result[0];i <= result[1];i++) {System.out.print (A[i]+ "\ T"); } System.out.println ("And as:" + result[2]); //The array is connected to a ring, and then the largest contiguous subarray of this array is computed.//Connect to a ring the array is then connected, but the last element of the array is not connected, which is equivalent to the sum of the largest subarray of the array after the connection is computed . int[] B =New int[2 * n-1]; for(inti = 0;i < n-1;i++) {B[i]=A[i]; B[n+ i] =A[i]; } b[n-1] = a[n-1]; int[] Result2 =maxsub (b,n); System.out.println ("\ n \ nyou Connect the array to the largest contiguous subarray after the loop:"); for(inti = Result2[0];i <= result2[1];i++) {System.out.print (B[i]+ "\ T"); } System.out.println ("And as:" + result2[2]); } //computes the and largest contiguous subarray of a array (a array is an equivalent array after a loop, that is, twice times the original array, and n is the length of the original array) Public Static int[] Maxsub (int[] A,intN) {intan = A.length;//The length of an equivalent array of rings intCurrectsum = a[0];//record current cumulative sum, initial value is a[0] intCurrectstartindex = 0;//record the starting subscript for the current summation, with an initial value of 0 intCount = 1;//record the number of accumulated elements with an initial value of 1 int[] result =New int[3];//records the information of the result Subarray,Result[0] = 0;//start subscript for result Sub-arrayRESULT[1] = 0;//end subscript for result Sub-arrayRESULT[2] = a[0];//the and of the result sub-array for(inti = 1;i < an;i++)//iterate through each element of the a array in turn { if(currectsum <= 0)//if the current sum is not greater than 0, no more than 0 for the subsequent elements do not contribute, can be removed, so should be a[i] place to restart the add{currectsum= A[i];//the current summation and assignment is a[i]Currectstartindex = i;//assigns the starting subscript of the current cumulative value to ICount = 1;//Count the number of accumulated elements as 1 } Else//current cumulative and greater than 0, continue to add a[i]{currectsum+=A[i]; Count++;//the number of current add-on elements plus one } if(Currectsum > Result[2])//If the current sum and is greater than the sum and result[2 of the original result array, the result subarray information should be updated to the current subarray, because the sum of the current sub-array and greater than the result sub- array{result[0] = Currectstartindex;//The start subscript of the result sub-array is the start subscript of the current sub-arrayRESULT[1] = i;//the end subscript of the result sub-array is assigned the value IRESULT[2] = currectsum;//The summation and assignment of the result sub-array is the sum of the current Subarray and } if(Count >= N)//if the sum of the number of elements equals the length of the original array (an array not connected to the ring), then the most added element is not added, that is, the largest sub-array is the original array, should end the loop { Break; } } returnresult; } }
Experiment:
Java one-dimensional array jobs