Cyclic two and maximum sub-arrays

Source: Internet
Author: User

1. Design Ideas

Combining the one-dimensional subarray of the loop with the two-dimensional sub-array, because my two-dimensional maximum sub-array and calculation, the two-dimensional array of the largest and converted into a one-dimensional array; therefore, combining these two ideas, you can easily write two-dimensional and maximum sub-arrays.

2. Source code

#include <iostream>

#include <ctime>

using namespace Std;

int main ()

{

int m, n; Array size

int I, J, p, Q, O; Loop variable

int a[100][100]; Array

int b[100][200]; Replace the loop matrix

int sh, x; Range of values

int sum, sum1 = -100, sum2 = 100; Calculation and

int max = a[0][0]; Maximum Value

int s[200]; into an array of

int k = 0; variable that controls the number of rows

cout << "Please enter the number of rows in the matrix (upper 100):";

CIN >> m >> N;

cout << "Please enter a range of values (A-B):";

CIN >> SH >> x;

cout << "original matrix:" << Endl;

Srand ((unsigned) time (NULL));

for (i = 0; i<m; i++)

{

for (j = 0; j<n; j + +)

{

A[I][J] = rand ()% (X-sh + 1) + sh;

cout << A[i][j] << "";

}

cout << Endl;

}

for (i = 0; i < m; i++)

{

for (j = 0; J < 2 * N; j + +)

{

int d = j;

if (d>=n)

{

D = d-n;

}

B[I][J] = a[i][d];

}

}

for (j = 0; J < 2 * N; j + +)

{

S[J] = 0;

}

for (i = 0; i<m; i++)

{

while (k + i<m)

{

for (j = 0; J < 2 * N; j + +)

{

S[J] = S[j] + b[k + i][j];

}

for (int l = 0; l < n; l++)

{

for (P = l; p < n + L; p++)

{

for (q = l; q <= p + L; q++)

{

sum = S[q + L];

for (o = q + L + 1; o <= p; o++)

sum = sum + s[o];

if (Sum > Sum1)

{

sum1 = sum;

}

}

if (Sum1 > Sum2)

{

sum2 = sum1;

}

}

}

if (sum2 > Max)

{

max = sum2;

}

k++;

}

k = 0;

for (j = 0; J < 2 * N; j + +)

{

S[J] = 0;

}

}

cout << "and maximum sub-matrix values are" << max << Endl;

return 0;

}

3.

4. Summary

This time the small program is also in the original code to modify, and is completely borrowed from one of the code, the modification is very simple, and further explain the importance of design ideas, but also explain the importance of annotations, can teammates quickly see their own procedures, more conducive to team cooperation, improve the efficiency of the work.

Cyclic two and maximum 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.