To find the two-dimensional chapter of the smallest sub-array

Source: Internet
Author: User

First, design ideas

Find all the sub-arrays of the two-dimensional array, first determine a position as the starting point, and then to the right and then start with this starting point of all the sub-arrays,

Figure 1-Order sub-array

Specifically, such as 1, the sub-array is calculated in order, and then compared to the max value, if it is larger than the max value, then the sub-array and assigned to Max, and save its position, the location of the sub-array, only need to save its end-to-end position,

Figure 2-saving the sub-array position

As shown in 2, the maximum subarray and its position are obtained, and the output can be.

Second, the Code

1  PackageZishuzu;2 3 ImportJava.util.*;4 5  Public classZuixiaozishuzu {6 7      Public Static voidMain (string[] args) {8         //TODO auto-generated Method Stub9 Ten         intm,n,m,n,max,sum; One         inti,i1,i2,j,j1,j2; A         intShoul,shour,weil,weir; -Scanner sc =NewScanner (system.in); -System.out.println ("Enter the number of rows and columns of the two-dimensional array:"); them =sc.nextint (); -n =sc.nextint (); -System.out.println ("Enter the range of values for the two-bit array (to ensure that the first number is less than the second):"); -M =Sc.nextint ();  +N =sc.nextint (); -          +         int[] Shuzu =New int[m][n]; A          for(i = 0;i < m;i++) at              for(j = 0;j < n;j++) -             { -SHUZU[I][J] = N + (int) (Math.random () * (M-N)); -             } -System.out.println ("The random two-dimensional array is:"); -          for(i = 0;i < m;i++) in              for(j = 0;j < n;j++) -             { toSystem.out.print (shuzu[i][j]+ "\ T"); +                 if(j = = N-1) -                 { theSystem.out.print ("\ n"); *                 } $             }Panax Notoginseng          -Sum =0; themax = Shuzu[0][0]; +ShouL = 0; AShour = 0; theWeiL = 0; +WeiR = 0; -  $          for(i = 0;i < m;i++) $         { -              for(j = 0;j < n;j++) -             { the                  for(I1 = I;i1 < m;i1++) -                 {Wuyi                      for(J1 = j;j1 < n;j1++) the                     { -                          for(I2 = I;i2 <= i1;i2++) Wu                         { -                              for(J2 = j;j2 <= j1;j2++) About                             { $sum = sum +Shuzu[i2][j2]; -                             } -                         } -                         if(Max <=sum) A                         { +Max =sum; theShouL =i; -Shour =J; $WeiL =I1; theWeiR =J1; the                         } thesum = 0; the                     } -                 } in             } the         }         the          AboutSYSTEM.OUT.PRINTLN ("Maximum sub-array and as:"); the System.out.println (max); theSYSTEM.OUT.PRINTLN ("Maximum sub-array is:"); the          for(i = shoul;i <= weil;i++) +              for(j = shour;j <= weir;j++) -             { theSystem.out.print (Shuzu[i][j] + "\ T");Bayi                 if(J = =WeiR) the                 { theSystem.out.print ("\ n"); -                 } -             } the          the sc.close (); the}
View Code

Third, the experimental results

Figure 3-Result 1

Figure 4-Result 2

Figure 5-Result 3

Iv. Development process

A two-dimensional array, when the smallest sub-array is seen, the idea is to find out the two-dimensional array of each sub-array, although the idea of common, high time complexity, but also do not consider a lot of special circumstances, the risk of error is much lower.

At the beginning of the design is a time complexity for the n6 of a train of thought, the first layer is the outermost is about the starting position of the loop, the second layer is about the end of the loop, the innermost is the sub-array of the self-summation, each layer has two for loop.

At the beginning of the writing, the loop when the value is set to I,II,III,J,JJ,JJJ, the runtime has failed, took two lessons how can not find the error, suddenly reminded of the companion's reminder: Why not i,i1,i2,j1,j1,j2, so it is not easier to see, so will all the values come over, Change to the last JJJ value, only to find that I was "JJJ" written "JJ", resulting in the program's Infinite Loop.

In the two lessons of the mistake, the companion reminded that to reduce the complexity of the time, it is to modify each layer of the For loop, reduce one, write i++ in the For loop, the specific code is as follows:

1i = 0;2          for(j = 0;j < n;j++)3         {4I1 =i;5              for(J1 =j;j1 < n;j1++)6             {7I2 =i;8                  for(J2 = j;j2 <= j1;j2++)9                 {TenSum + =Shuzu[i2][j2]; One                     if(J2 = = J1) && (I2 <i1)) A                     { -i2++; -J2 =J; the                     } -                     Else if(J2 = = J1&&i2 = =i1) -                     { -                          Break; +                     } -                 } +                 if(Max <sum) A                 { atMax =sum; -ShouL =i; -Shour =J; -WeiL =I1; -WeiR =J1; -                 } insum = 0; -                 if(J1 = = n-1 && I1 < m-1) to                 { +i1++; -J1 =J; the                 } *                 Else if(J1 = = N-1 && J1 = = M-1) $                 {Panax Notoginseng                      Break; -                 } the             } +             if(j = = N-1 && J < m-1) A             { thei++; +j = 0; -             } $             Else if(j = = N-1 && j = m-1) $             { -                  Break; -             } the}
View Code

Can run out, can be tested when the results are found to be wrong, so first used the first method.
The program can also be slightly improved, that is, when the largest Subarray and the same sub-array are present, all the arrays are output and can be saved with an array.

V. Pair development Members

Liu Shuang Bo, Liu Hongyang

To find the two-dimensional chapter of the smallest 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.