First, design ideas
1. The circle (int l) function makes an input to the array length.
2. The SetArray () function makes an array input.
3. The Changearray () function converts the array loop to the maximum value of the ring at which Max is calculated
First, assign the value of the array[] array to the sarray[] array and store the original value. The For loop then finds the minimum value for this array.
The position of this minimum value is then obtained through the loop. Then after assigning the number of positions to the new array Narray array before the position of the assignment to Narray, get a minimum value in the first place of the new array.
4.
The ArrayMax () function implements the maximum number of sub-arrays. The complexity should be O (n), first enter an array such as:
-1 2-1-2 5 4 3-6 8 9
Define a new array carray[], length 11. Then initialize CARRAY[1] for the 0;for loop.
CARRAY[I+1] = Carray[i]+array[i];
So the new array is:
0-1 2 1-1 ...
Because the maximum value is determined, it is judged that when carray[] is less than 0 o'clock, the value of 5 is assigned to the new array
That is:
0-1 2 1-1 5 9 12 6 ...
Finally, in the new array obtained, the maximum value of this is the maximum subarray and.
Source:
1 ImportJava.util.Scanner;2 Public classArray2 {3 4 Public Static voidMain (string[] args) {5 //TODO auto-generated Method Stub6SYSTEM.OUT.PRINTLN ("Please input your array length");7Scanner C =NewScanner (system.in);8 intClength =c.nextint ();9Circle S =NewCircle (clength);Ten S.setarray (); One S.changearray (); A S.arraymax (); - } - the } - classCircle - { - intArray[];//Original Array + intSarray[];//storing the original array - intNarray[];//a new array after conversion + intCarray[];//the last obtained array A intLength//Array Length at intMax; - intmin; - intposition; - inti; - intm; - PublicCircleintL//Input Array Length in { -Length =l; to } + - voidSetArray ()//Input Array the { *SYSTEM.OUT.PRINTLN ("Please input your array"); $Array =New int[length];Panax NotoginsengScanner s =NewScanner (system.in); - for(inti=0;i<length;i++) the { +array[i]=s.nextint (); A } the } + - voidChangearray ()//convert array ring to array column at this point max is the maximum value to be calculated $ { $ //storing the original array -Sarray =New int[length]; - for(I= 0;i<length;i++) the { -Sarray[i] =Array[i];Wuyi } the - //to get the minimum value in array arrays I Wu for(i=length-2;i>=0;i--) - { About if(array[i]<array[i+1]) $ { -Min =Array[i]; - } - Else if(array[i]>=array[i+1]) A { +m =Array[i]; theArray[i] = array[i+1]; -ARRAY[I+1] =m; $Min =Array[i]; the } the } the the //find the minimum value position - for(i=0;i<length;i++) in { the if(sarray[i]==min) the { AboutPosition =i; the } the } the + //get a new array -NArray =New int[length]; the for(i=0;i<length-position;i++)Bayi { theNarray[i] = sarray[position+i]; the } - for(i=length-position;i<length;i++) - { the if(length-position-i>0) the { thenarray[i]=sarray[length-position-i]; the } - Else if(length-position-i<=0) the { thenarray[i]=sarray[i+position-length]; the }94 } the the } the voidArrayMax ()//Maximum output value98 { AboutCArray =New int[Length+1]; -Carray[0] = 0;101 for(i=0;i<length;i++)102 {103 if(carray[i]>=0)104 { theCARRAY[I+1] = carray[i]+Narray[i];106 } 107 while(carray[i+1]<0)108 {109Carray[i+2]=narray[i+1]; thei++;111 } the }113 //get the maximum value in the array the for(i=1;i<length;i++) the { the if(carray[i+1]>=Carray[i])117 {118max = Carray[i+1];119 } - Else if(carray[i+1]<Carray[i])121 {122m =Carray[i];123Carray[i]=carray[i+1];124carray[i+1]=m; themax = Carray[i+1];126 }127 } -System.out.println ("Max is" +max);129 } the}
Three
Iv. Summary
Unit testing is important to find bugs in some function functions.
Classroom exercises--maximal subarray and ring