A two-dimensional array of maximum connectivity sub-arrays and

Source: Internet
Author: User

1. Design idea: The user keyboard input a custom several rows of a few columns of the matrix, and then enter the relevant values, and then call the two-dimensional array of maximum connectivity sub-array and the method, the final output maximum value.

2. There is a problem: the idea of a sub-array of the unicom is not very understanding, unclear.

3. Solution: The maximum and minimum blocks of each row are first asked, then the total maximum value is then output; when the number of columns is 1 o'clock, it is calculated as one dimension;

4. Source code:

/*returns the and of the largest sub-array in an integer array. 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. */ PackageShuzumax;ImportJava.util.Scanner; Public classshuzuu{ Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); System.out.println ("Please enter the rows and columns of the two-dimensional array:"); inthang=Scanner.nextint (); intLie=Scanner.nextint (); int[] shuzu=New int[Hang][lie]; inti,j;  for(i=0;i)        {             for(j=0;j<lie;j++) {Shuzu[i][j]=Scanner.nextint (); }} System.out.println ("The maximum number of consecutive and continuous values in this array is:" +Max (Shuzu,hang,lie)); }      Static intMaxintShuzu[][],intHangintlie) {        intMax=0,sum=0; inti,j; int[] b=New int[Lie]; int[] up=New int[Hang]; int[] down=New int[Hang]; int[] t=New int[Hang]; if(lie==1)        {             for(i=0;i) {sum+=shuzu[i][0]; if(sum<0) {sum=0; }                if(sum>max) {Max=sum; }            }           if(max==0)           {                for(i=0;i)               {                   if(i==0) {Max=shuzu[i][0]; }                   if(shuzu[i][0]>max) {Max=shuzu[i][0]; }               }            }        }        Else        {             for(i=0;i)            {                 for(j=0;j<lie;j++) {B[j]=Shuzu[i][j]; }                int[] c=New int[100]; c[0]=0; intSum1=0,max1=0, K;  for(k=0;k<lie;k++)                {                    if(sum1<0) {sum1=B[k]; }                    Else{sum1=sum1+B[k]; } C[k]=sum1; } max1=c[0]; intMmark=0,smark=0;  for(k=0;k<lie;k++)                {                    if(max1<C[k]) {Max1=C[k]; Mmark=K; }                }                 for(k = mmark;k >= 0;k--)                {                    if(C[k] = =B[k]) {Smark=K;  Break; }} sum=Max1; Up[i]=Smark; Down[i]=Mmark; T[i]=sum; }            intT2=t[0];  for(i=0;i)            {                if(Up[i]<=down[i+1] && down[i]>=up[i+1]) {T2+=t[i+1]; }                 for(j=up[i];j<up[i+1];j++)                {                    if(shuzu[i+1][j]>0) t2+=shuzu[i+1][j];//distinguishing independent positive numbers}} Max=T2; }        returnMax; }} 

5.:

6. Summary: Through this experiment, I think I understand the two-dimensional array is not deep enough, for the problem to a layer of analysis, the problem can be simplified, in order to progress.

A two-dimensional array of maximum connectivity sub-arrays and

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.