A two-dimensional array loop for maximal sub-arrays

Source: Internet
Author: User

1. Experimental requirements

Randomly produces an array of two-dimensional shapes, with positive numbers in the array and negative values.

A two-dimensional array is connected to the end of the line, like a belt.

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.

2. Experimental ideas

First, a two-dimensional array is generated randomly;

The first column of a two-dimensional array is moved to the last column, and the largest subarray is obtained;

The first column of a two-dimensional array is moved to the last column, and the largest subarray is obtained;

The first column of a two-dimensional array is moved to the last column, and the largest subarray is obtained;

······

Until the loop is complete.

3. Complete code

ImportJava.util.Random;ImportJava.util.Scanner; Public classTwoarray2 {/**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub         inta[][]; intI,ii,j,jj,l,h,n,nn,k,kk,sum,max=0; ///randomly generates an arrayScanner Scanner =NewScanner (system.in); System.out.print ("Output the length of the randomly generated array:"); H=Scanner.nextint (); System.out.print ("Output the height of the randomly generated array:"); L=Scanner.nextint ();            Scanner.close (); A=New int[L][h];  for(i=0;i<l;i++)            {                 for(j=0;j) {Random random=NewRandom (); A[I][J]=random.nextint (19); A[I][J]=a[i][j]-9;                    System.out.print (A[i][j]); System.out.print (" "); } System.out.println (""); }                          for(nn=0;nn)            {                 for(ii=0;ii<l;ii++)//L PUT the first number of rows to the best{KK=a[ii][0];  for(jj=0;jj//the first number of each row is the best{A[II][JJ]=a[ii][jj+1]; } A[II][JJ]=KK; } System.out.println ("Put the first column of the array into the last column:");  for(i=0;i<l;i++)/////////aaa An array after the output is moved                {                     for(j=0;j) {System.out.print (a[i][j]); System.out.print (" "); } System.out.println (""); }////////////////////////AAA output Array after the move//max=0;                 for(n=0;n<l;n++) {sum=0;  for(i=0;i<l-n;i++) {sum=0;  for(j=0;j)                        {                             for(k=i;k<=i+n;k++) {sum=sum+A[k][j]; }                            //System.out.print (sum); //System.out.print ("");                            if(sum<0) {sum=0; }                            if(max<sum) {Max=sum; } }}} System.out.print ("The maximum and the number of sub-arrays currently obtained is:");            SYSTEM.OUT.PRINTLN (max); }    }}

4.

5. Summary

We have adopted the most basic method, but we have used several methods to verify the results to make the operation more reliable.

A two-dimensional array loop for maximal sub-arrays

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.