Title: Returns the and of the largest subarray in an array of one-dimensional integers.
Requirements:
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.
This He Linlin is responsible for program analysis, code programming, Zhang Yibo responsible for code review and Code test plan.
The analysis and discussion of the design ideas are as follows:
1, the definition of a 20-length array, the first 10 numbers are initialized, the next 10 numbers equals the first 10 numbers;
2, set the length of 10 of the loop in turn, the previous 10 number is the head of the maximum sub-array calculation;
3, then set an array to store the maximum number of sub-arrays produced per loop, and then take the maximum
4, output maximum value
Source
1 PackageArray;2 3 ImportJava.util.*;4 Public classTest {5 6 Public Static voidMain (string[] args) {7 //TODO Auto-generated method stubs8 int[]shuzu=New int[20]; 9 int[]max=New int[10];Ten intI,sum=0; One //initializing an array A for(i=0;i<10;i++) - { -System.out.println ("Please enter" + (i+1) + "values"); theScanner input=NewScanner (system.in); -shuzu[i]=Input.nextint (); - } - for(i=10;i<20;i++) + { -Shuzu[i]=shuzu[i-10]; + } A intW=0; at for(intx=0;x<10;x++) - { -sum=Shuzu[x]; - for(i=x;i< (x+10); i++) - { - if(w<0) in { -w=Shuzu[i]; to } + Else - { thew+=Shuzu[i]; * } $ if(sum<W)Panax Notoginseng { -sum=W; the } + } AW=0; themax[x]=sum; +System.out.println ("The maximum sub-array of each subarray and is:"); - System.out.println (max[x]); $ } $ intMax1=max[0]; - for(intq=0;q<10;q++) - { the if(max[q]>max1) - {Wuyimax1=Max[q]; the } - } WuSYSTEM.OUT.PRINTLN ("Maximum sub-array and" +max1); - About } $}
Results:
Cooperation experience: two times the basic idea of the program is not much, for W as the sum of the intermediary, in each different first to find the largest sub-array and complete, must be initialized to 0, otherwise it will occur and overlay.
Software Engineering class Exercise: Returns the maximum number of sub-arrays in an array of one-dimensional integers and