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