A two-dimensional array loop to find the largest sub-array

Source: Internet
Author: User

 PackageShengcheng;ImportJava.util.Scanner;Importorg.junit.Test; Public classErweixunhuan {@Test Public voidFun () {int[][]a=New int[4] [8]; Scanner Write=NewScanner (system.in);  for(inti=0;i<4;i++)    {         for(intj=0;j<4;j++) {A[i][j]=Write.nextint (); A[i][j+4]=A[i][j]; }    }        intmax=-200000; int[]sum=New int[8]; intAdd =-200000 ;  for(inti=0;i<4;i++)    {         for(intl=0;l<8;l++) {Sum[l]=0; }         for(intj=i;j<4;j++)        {                                        for(intk=0;k<8;k++) {Sum[k]+=A[j][k]; } Add=DP (sum,4); }                if(add>max) {Max=add;                    }} System.out.println (max); }  Public  intDP (intA[],intN) {intMax=a[0]; inttemp=-200000;  for(inti=0;i<n;i++) {Temp=-200000;  for(intj=i;j<n+i;j++) {Temp+=A[j]; if(temp>Max) {Max=temp; }                          if(temp<0) {Temp=A[j]; }                       }        }     returnMax; }            }

Teacher out of this topic, I first think of the original one-dimensional array loop to find the largest sub-matrix, and then think of the original two-dimensional array to find the maximum sub-matrix, feel can use the original idea to continue thinking, so I thought the original two-dimensional array of the largest sub-matrix is also the two-dimensional conversion to one-dimensional And then the one-dimensional array of dynamic planning, so that the original two-dimensional problem, so I used the same method to expand the number of two-dimensional column as a result of the cycle, and then the two-dimensional array into a one-dimensional array, and then this can be used on the original one-dimensional loop array to solve the problem.

A two-dimensional array loop to find the largest sub-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.