The sum of the maximal sub-arrays in the ring array

Source: Internet
Author: User

The sum of the maximal sub-arrays is calculated by the ring array:

Experimental requirements:

Randomly produces an array of shapes, assuming that the end-to-end is connected to a ring, the sum of the concatenated word groups is obtained, and the elements of the sub-array are output.

Design ideas:

Because it is a ring, so we should consider the situation of the tail-up, which is divided into two categories of consideration, a positive number in the array, a case of all negative;

In the case where there are positive numbers can be divided into three cases, one for all positive, one for the self-set head element is positive, the last is a self-set head element is negative. The implementation is based on the classification of these conditions.

In the implementation process, the array elements are traversed from beginning to end, if a positive number is encountered and added, until adjacent to the next negative, that is, the largest array is stored and the last few adjacent positive numbers and compared to find the largest.

The special case for the head element is a positive number, that is, first the sum of the array of head elements, and then consider whether the Mantissa group is positive, if it is to store the Mantissa group and, the last array and add, that is, the sum of the connected array.

Code implementation:

 PackageThe sum of the ring sub-arrays;ImportJava.util.Scanner; Public classHX {/**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner A=NewScanner (system.in); System.out.println ("Please enter the array length:"); intn=A.nextint (); System.out.println ("Please enter array range of values:"); intN =A.nextint (); intM =A.nextint (); intA[] =New int[n]; intB[][] =New int[N][n]; inti; System.out.println ("Array is:");  for(i=0;i<n;i++) {A[i]= N + (int) (Math.random () * (M-N)); System.out.print (A[i]+ "\ T");                } System.out.println (); Booleanx =false;  for(i=0;i<n;i++)        {            if(a[i]>=0) {x=true;  Break; }        }                intSum=0,s1=0,s2=0,loc=0,loc1=0; if(x = =true)//in case of positive        {             for(i=0;i<n;i++)//in cases where all are positive{s2+=A[i]; if(a[i]>=0) Sum+=A[i]; if(sum==S2) {S2=sum; Loc1=N; }                Else                     Break; }            if(a[0]>=0)//the first number at the beginning of the default is positive{sum=a[0];  for(i=1;i<n;i++)                {                    if(a[i]>=0) {sum+=A[i]; }                    Else                    {                    if(sum>=s1) S1=sum; Loc=i; if(a[i]<0) Sum=0;  Break; }                }                                intSum1=0;  for(i=0;i<n;i++)                {                    if(a[i]>=0) {sum1+=A[i]; }                    Else                     {                        if(sum1>=S2) {S2=sum1; Loc1=i; }                        if(a[i]<0) sum1=0; }                }                intS3=0,loc2=0; if(a[n-1]>=0) {sum=a[n-1];  for(i=n-2;i>=0;i--)                    {                        if(a[i]>=0) {sum+=A[i]; }                        Else{S3=sum; LOC2=i;  Break;} }                }                //System.out.print (": S" +s1+ "" +s2+ "" +s3 "); //output Results Test the intermediate process is correct//System.out.print (":" +loc+ "" +loc1+ "" +loc2 ");                ints4=s1+S3; if(s2>=S4) {System.out.print ("Maximum sub-array and as:" +S2);                    System.out.println (); System.out.println ("Sub-array is:");  for(i=0;i<loc1;i++)                    {                        if(a[i]>0) {System.out.print (A[i]+ "\ T"); }                        Else                              Break; }                }                Else{System.out.print ("Maximum sub-array and as:" +S4);                    System.out.println (); System.out.println ("Sub-array is:");  for(i=0;i<loc;i++) {System.out.print (A[i]+ "\ T"); }                     for(i=n-1;i>loc2;i--) {System.out.print (A[i]+ "\ T"); }                }            }            Else                  //The default first number is negative{sum=0;  for(i=0;i<n;i++)                {                    if(a[i]>=0) {sum+=A[i]; if(i==n-1&&sum>s1) S1=sum; Loc=i; }                    Else                     {                        if(sum>=S1) {S1=sum; }                        if(a[i]<0) Sum=0; }} System.out.println ("LOVC" +Loc); System.out.print ("Maximum sub-array and as:" +S1);                System.out.println (); System.out.println ("Sub-array is:");  for(i=loc;i>=0;i--)                {                    if(a[i]>0) {System.out.print (A[i]+ "\ T"); }                    Else                          Break; }            }        }        Else if(x = =false)//If there are no positive cases        {            intT=a[0];  for(i=1;i<n;i++)            {                if(a[i]>t) {T=A[i]; }} System.out.print ("Maximum sub-array and as:" +t);            System.out.println (); System.out.println ("Sub-array is:");        System.out.print (t); }    }}

Experimental results:

Results Analysis:

During the experiment, it is simple to generate random numbers and the idea process, but in the process of implementation, there are still a lot of situations, which define a Boolean x = false; a type that uses an equal sign in an if statement without error, but skips directly during the run. Run with two equals sign.

When the sum of the array is stored with sums , because there is a loop, it always changes, and the first store for all positive and, not continuous, plus an else statement and then storage and can be stored maximum, break is a good deal to jump out when you encounter negative numbers.

When outputting an array element, an array position representing the maximum sum is used , and the required elements are output at the end of the loop.

Knot pair Implementations:

Liu Hongyang, Liu Di bo: Joint discussion, the joint study of the implementation of the method, the respective completion of the code part, and then compared to, find the advantages of mutual absorption.

The sum of the maximal sub-arrays in the ring array

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.