The largest subarray of one-dimensional arrays and

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.