The title is: An array of one-dimensional loops to find out the maximum number of sub-arrays
The teacher just give this topic, find out one-dimensional sub-array of the largest sub-array of the and, at that time I would like to have done a one-dimensional array to find the largest sub-array and can be extended on this basis, so I want to use the original algorithm, Later I thought that since it is still to find the largest sub-array and certainly the original things can be exploited.
I think since it is the loop, nothing is this array is carried out two times, so I feel so that I can apply to the array after the same length, the number of the size and order and its same array, so it played the purpose of the loop, so I did so, and then call the original method, This allows you to find the value of the maximum subarray. So I wrote out the program and the results were found to be wrong. The most sub-array and not the original is not the expected result, so I think the problem is where, and then I found that the original problem is the length of the largest sub-array is a problem, since the length of the array has been enlarged one-fold, so the length of the sub-array may also be changed, At this point I thought of the need to add some restrictions on the length of the sub-array, so that his length is always less than the length of the original array, so that a good solution to the problem of the length of the sub-array, so that the sub-array is in line with the requirements.
PackageShengcheng;ImportJava.util.Scanner;Importorg.junit.Test; Public classxunhuan{@Test Public voidFun () {int[]a=New int[10]; intN; Scanner Write=NewScanner (system.in); System.out.println ("Please enter the length of the array to be entered"); N=Write.nextint (); for(inti=0;i<5;i++) {A[i]=Write.nextint (); } Xunhuan (A,n); } Public voidXunhuan (intA[],intN) {inttemp=-200000; intmax=-200000; for(inti=0;i<n;i++) {A[i+n]=A[i]; } for(inti=0;i<n;i++) {Temp=-200000; for(intj=i;j<n+i;j++) {Temp+=A[j]; if(temp>Max) {Max=temp; } if(temp<0) {Temp=A[j]; }}} System.out.println (Max); }}
Operation Result:
An array of one-dimensional loops to find out the maximum number of sub-arrays