First, the design idea: the same as the largest subarray of the array, iterate through all the sub-arrays, and then determine whether the last sub-array is greater than 0. If it is greater than 0, the traversal begins again until the sub-array is less than 0 or to the previous number of the last Subarray, where the maximum and position of the sub-array is saved.
Second, the code:
Import Java.util.scanner;public class Main {/** * @param args */public static void main (string[] args) { TODO auto-generated method Stub int i; int s=0,sum=0,head=0,end=0,h=0,e=0; Scanner sc=new Scanner (system.in); System.out.print ("Please enter array length:"); int X=sc.nextint (); int a[]=new int[x]; System.out.print ("Please enter the number in the array:"); for (i=0;i<x;i++) {a[i]=sc.nextint (); } for (i=0;i<x;i++) {s+=a[i]; if (s>0) {e++; } else {s=0; h=i+1; e++; } if (s>sum) {sum=s; Head=h; End=e; }} if (s>0) {head=h; i=0; E=e-x; while (s>0&&e!=h-1) {s+=a[i]; i++; e++; if (s>sum) {sum=s; End=e; }}} System.out.print ("The maximum number of sub-arrays is:"); SYSTEM.OUT.PRINTLN (sum); System.out.print ("Maximum sub-array is:"); if (End>head) {for (i=head;i<end;i++) {System.out.print (a[i]); System.out.print (""); }} else {for (i=head;i<x;i++) {System.out.print (a[i]); System.out.print (""); } for (i=0;i<end;i++) {System.out.print (a[i]); System.out.print (""); } } }}
Iii. results:
Iv. Summary: In this task I am responsible for testing aspects. By discussing with Guo Hao the program How to write this program has improved my awareness of the overall thinking, and I later in the programming of more perfect thinking has a great help. Secondly, I and Guo Hao classmates have the courage to point out the shortcomings of each other and let us in the team work together to unify the character indentation and other programming habits. This helps us to develop the way of programming,
Five, Knot group member: DUYONGSU (responsible for testing), Guo Hao (responsible for programming)
The largest subarray of one-dimensional arrays and