3.1 classroom exercises

Source: Internet
Author: User

Question: return the sum of the largest subarrays in a two-dimensional integer array.

Requirements:

Enter a two-dimensional integer array with positive or negative values.

A child matrix in a two-dimensional array forms a sub-array, and each sub-array has a sum.

Returns the maximum value of the sum of all sub-arrays. The time complexity is O (n ).

Thinking process: the biggest array of two-dimensional arrays is extended from one-dimensional arrays. First, determine the maximum upper and lower bounds and determine the maximum range of sub-arrays from the first line, for the range of the specified maximum sub-array, divide the maximum sub-array into several different rows according to the number of columns. For the arrays generated above, to obtain the maximum sum of sub-arrays.

1 # include <iostream> 2 using namespace STD; 3 void main () 4 {5 int A, B, I, J, m = 0, a [0, 100] [100]; 6 7 cout <"input matrix rows and columns"; 8 CIN> A> B; 9 if (a> 100 | B> 100) 10 {11 cout <"Enter again:"; 12 CIN> A> B; 13} 14 for (I = 0; I <A; I ++) 15 {16 for (j = 0; j <B; j ++) 17 {18 CIN> A [I] [J]; 19} 20 21} 22 int sum [100] = {0}, max = 0, result = A [0] [0]; 23 24 for (I = 0; I <A; I ++) 25 {26 while (m + I <A) 27 {28 for (j = 0; j <B; j ++) 29 {30 sum [J] = sum [J] + A [M + I] [J]; 31 32} 33 max = 0; 34 for (j = 0; j <B; j ++) 35 {36 IF (MAX + sum [J]> sum [J]) 37 {38 max = MAX + sum [J]; 39} 40 else41 {42 max = sum [J]; 43} 44 If (max> result) 45 {46 result = max; 47} 48} 49 m ++; 50} 51 m = 0; 52 for (j = 0; j <B; j ++) 53 {54 sum [J] = 0; 55} 56 57} 58 59 cout <result; 60}

Perception

: Each input code is a learning process, and problems can only be found after being done.

 

3.1 classroom exercises

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.