Topic:
Enter a one-dimensional shape array with positive and negative numbers in the array.
One-dimensional arrays end-to-end, like one end-to-end tape.
One or more consecutive integers in an array make up a sub-array, each of which has a and.
The maximum value for the and of all sub-arrays.
Publish a blog post about design ideas, problems that arise, possible solutions (multiple choices), source code, results, summaries.
Design ideas:
1. Design ideas:
First set up two variables, respectively, to store the length of the array is number and result, and then through the statement to ask the user for the length of the array and then set a dynamic array for the user to enter the numbers in the array, each value is stored in the array corresponding position, Set a number-length array max[number] to hold the maximum value for each iteration, and then use two loops to nest, the first loop is the start position, starting from i=0, until I=number, each time the corresponding max[i] is initially a[i],result =0, followed by the inside of the second loop, starting from J=i, execute number times, each time let Result=result+a[i], then judge the following conditions: if Result>max[i], then let Max[i]=result; , let result=0, if j==number-1, if the j=-1;> if j==i-1, then the end of this cycle. Make result=max[0], using a loop to determine the maximum value in the Max array, which is the maximum of the subarray, and the result is returned.
2, Source code:
1 //Majianning written in 2017.3.31 class assignments2 Packageclassroom testing;3 ImportJava.util.*;4 Public classCS25 {6 Public Static voidMain (String args[])7 {8 intNumber = 0,result = 0;9@SuppressWarnings ("Resource")TenScanner in=NewScanner (system.in); OneSystem.out.print ("Please enter the desired array length:"); ANumber =in.nextint (); - - intA[] =New int[number]; the intMax[] =New int[number]; -System.out.print ("Please enter an array of length" +number+ "(Input range: natural number):"); - for(inti = 0;i<number;i++) - { +A[i] =in.nextint (); - } + A for(inti = 0;i < number;i++) at { -Max[i] =A[i]; -result = 0; - for(intj = I;j < number;j++) - { -result=result+A[j]; in - if(Result >Max[i]) to { +Max[i] =result; - } the if(Result < 0) * { $result = 0;Panax Notoginseng } - the if(j = = Number-1) + { Aj =-1; the } + if(j = = I-1) - { $ Break; $ } - } - } theresult = Max[0]; - for(inti = 1;i<number;i++)Wuyi { the if(Max[i] >result) - { Wuresult =Max[i]; - } About } $ -SYSTEM.OUT.PRINTLN ("Maximum sub-array and as:" +result); - } -}
3. Running Results
The largest array of classroom assignments