Cooperation Process:
The last design ideas and programming language differences, through the communication discussion unity decided to use Java programming, Chen Peng responsible for programming, Li Guo responsible for code review and code testing.
Design ideas:
Set three variables, Max is (the maximum value in the Subarray and the initial value is Array[j]), RESULT1 (the current sub-array and the initial value is array[j]);
Let the user set the length of the array first, and then input the array (the input array is judged by whether it is all negative);
Add a For loop to the end-to-end phase before traversing;
After the first bit from the array to begin traversing the array, max=max+array[i], if result<0, then the max=0; if Max>result, then Result=max;
Finally, when the loop is finished, the value of the last result is output.
Problems encountered:
When the initial value is arrary[0], the first digit value cannot be negative when it is entered;
The maximum number of outputs is a number of negative numbers when all are negative;
Source:
Package Shuzhi;import Java.util.scanner;public class Shuzuzhi {public static void main (string[] args) {//TODO Auto-genera Ted Method Stubscanner in=new Scanner (system.in); System.out.println ("Please enter array length:"); int N=in.nextint (); int array[]=new Int[n]; int result = 0; Boolean bool=false; System.out.println ("Please enter the corresponding integer:"); for (int i=0;i<n;i++) {array[i]=in.nextint (); } for (int i=0;i<n;i++) {if (array[i]>0) bool=true; }//Determine if all are negative if (bool)//Not all negative {//Circle of loop for (int j=0;j<n;j++) {int max= ARRAY[J]; int RESULT1 =array[j]; for (int i=0;i<n-j;i++) {max=max+array[i]; if (MAX>RESULT1) {Result1=max; } if (max<0) {max=0; }} RESULT=RESULT1; } } else//are all negative {int max=array[0]; for (int i=1;i<n;i++) {if (Max<array[i]) max=array[i]; } Result=max; } System.out.print ("The maximum value of the Subarray is:" +result);}}
:
Summarize:
Enter the initial value to understand whether the initial value needs to change, using the For loop to determine whether the scope is correct;
Classroom Exercises--7th week (two pairs)