Sub-array Maximum value 02

Source: Internet
Author: User
Tags array length

Cooperation Process:

because of the different idea of the last homework design, the unified design thought was adopted in the discussion. For this assignment we will discuss together, we use a two-dimensional array to complete the loop of a one-dimensional array of sub-arrays. I am responsible for coding, Li Ping is responsible for the code review and code testing.

Design ideas:

The maximum value of a subarray based on the last non-cyclic one-dimensional array.

1. Convert a one-dimensional array into a two-dimensional array, transforming it into a loop in disguise. That is, a two-dimensional array each exercise one-dimensional array to move forward one number.

2. Use the last thought to find the maximum value of each row of sub-arrays at once.

2.1. Define two number S,max to update the maximum value of the subarray, starting with the data[n-1] of the array data[] to make a summation comparison.

2.2 Use the For loop to determine whether the sum of the first few items is less than 0, and if less than 0 continue to look for the maximum value from the item and compare it to the maximum of the previous items. Select the maximum value.

3. Compare the size of the maximum value of each row of a two-dimensional array and compare it to the maximum output.

Problems encountered:

The conversion of one-dimensional array to a two-dimensional array occurs when an array crosses.

Source:

ImportJava.util.Scanner; Public classtest{Static int[] Transform (intData[],intN) {//Converts an array into a two-dimensional array that transforms a one-dimensional array into a loop array          intd[][]=New int[N][n]; intJ;  for(j=0;j<n;j++)//The first row holds a one-dimensional array{d[0][j]=Data[j]; }           for(inti=1;i<n;i++)//one step forward a number is stored in the next row of a two-dimensional array          {                for(j=0;j<n-1;j++)              {                  intT=d[i-1][0]; D[I][J]=d[i-1][j+1]; D[i][n-1]=T; }              }          returnD; }      Static intSumintData[],intN//defining arrays and array lengths      {          ints = data[n-1];//s used to update the maximum value of the Subarray          intmax = data[n-1];//a indicates the maximum value           for(inti=n-2;i>=0;i--)//reverse Order          {              if(s<0)//The first few items are negative and are recalculated{s=0; } s=s+Data[i]; if(s>max) {Max=s;//assign the maximum value to a              }          }           returnMax; }      Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner in=NewScanner (system.in); System.out.println ("Please enter the array length:"); intn=In.nextint (); intarray[]=New int[n]; inttwoarray[][]=New int[N][n]; System.out.println ("Please enter a" +n+ "Integer:");  for(inti=0;i<n;i++) {Array[i]=In.nextint (); }        //convert to two-bit arraytwoarray=transform (array,n); intresult[]=New int[n]; intchucun[]=New int[n];  for(inti=0;i<n;i++)        {             for(intj=0;j<n;j++)            {                //store two-dimensional arrays in one-dimensional array for each rowchucun[j]=Twoarray[i][j]; }            //calculate the maximum number of sub-arrays per rowresult[i]=sum (chucun,n); }        //find the maximum value in result        intMax=result[0];  for(inti=0;i<n-1;i++)        {            if(result[i+1]>Result[i]) {Max=result[i+1]; }} System.out.print ("The maximum number of sub-arrays is:" +max); }    }

Experimental results:

Summarize:

On the basis of the first program, you can simply implement an array loop. A two-dimensional array can be used to implement a one-dimensional array loop.

Sub-array Maximum value 02

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.