Returns the and (ring) of the largest sub-array in a one-dimensional array of integers

Source: Internet
Author: User

Topic:

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.

Design ideas:

(1) There are three one-dimensional arrays. The first array to store the input, the second to store the maximum value of the different sub-arrays, and the third to store the maximum values of the arrays with different starting points.

(2) 1, the number of custom input integers

2, create the first one-dimensional array, the digital input

3, using the loop, the number of the first array that exists, the addition calculation, if and is positive, continue to calculate, if and is negative, the next restart calculation. All the results are placed in the second one-dimensional array, the second array, the maximum value, into the third array.

4. In the first array, the different positions start with the calculation and repeat 3.

5. The maximum value of the third array is calculated. Output

Source:

ImportJava.util.Scanner; Public classcircleshuzu{ Public Static voidMain (String args[]) {Scanner Shu=NewScanner (system.in); System.out.println ("Please enter the number of integers:"); intCount=Shu.nextint ();  while(count<=0) {System.out.println ("Input error, please re-enter:"); Count=Shu.nextint (); }        intarray[]=New int[Count]; intcrray[]=New int[Count]; System.out.println ("Please enter an integer");  for(inti=0;i<count;i++) {Array[i]=Shu.nextint (); }             for(intj=0;j<count;j++)    {        intB=1; intbrray[]=New int[100]; brray[0]=Array[j]; if(j==count-1)        {             for(inti=0;i<count-1;i++)            {                if(brray[b-1]<=0) {Brray[b]=Array[i]; }                if(brray[b-1]>0) {Brray[b]=brray[b-1]+Array[i]; }                if(i==j-1)                {                     Break; } b++; }        }         for(inti=j+1;i<count;i++)        {            if(brray[b-1]<=0) {Brray[b]=Array[i]; }            if(brray[b-1]>0) {Brray[b]=brray[b-1]+Array[i]; }            if(i==count-1) {i=-1; }            if(i==j-1)            {                 Break; } b++; }        intMax1=brray[0];  for(inti=1;i<count;i++)        {            if(brray[i]>Max1) {Max1=Brray[i]; }} Crray[j]=Max1; }    intMax2=crray[0];  for(intm=1;m<count;m++)    {        if(crray[m]>Max2) {MAX2=Crray[m]; }} System.out.println ("Array Maximum value:" +Max2); }}

Results:

Cooperation Process Experience:

Through the discussion, we have improved the previous question, we used a two-dimensional array, this time we use a number of one-dimensional array to solve the problem, further from the different first item traversal, select the maximum value on the line.

Two people together to analyze the program, I write code, Wang Lin review, encountered errors, common solution.

Conflict Resolution:

When the first item is the last number, the loop that was previously designed is skipped, resulting in an error. So a separate calculation was given to the last Prime minister.

Summarize:

Two design ideas are similar. What has changed is to change the two-dimensional array to a one-dimensional array, with multiple arrays to store the desired values. Cooperative development can efficiently solve some problems, the direction can be more accurate.

Returns the and (ring) of the largest sub-array in a one-dimensional array of integers

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.