Design ideas
First, a two-dimensional array is generated by random number, which can control the range of generating random numbers, and the macro definition is used to represent the size of two-dimensional arrays with long and wide controls. The sum of the maximal subarray of the maximum sub-array and the need to take advantage of the previous blog (the sum of the largest subarray of one-dimensional array), the sum of the maximal sub-arrays of several rows is obtained by the method of line-by-row rotation, and a one-dimensional array is used to store the one-dimensional array which is added by line This transforms the two-dimensional array into a one-dimensional array, and the problem is solved.
SOURCE program code
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace Std;
#define M 4
#define N 4
void Main ()
{
int m=4,n=4,a[100][100],i,j,b,c,z;
cout<< "Please enter a range of values:" <<endl;
cin>>b>>c;
cout<< "Generate a two-dimensional array:" <<endl;
Srand (Unsigned (time (0)));
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
A[i][j]=rand ()% (c-b+1) +b;
cout<<a[i][j]<< "";
}
cout<<endl;
}
int sum,s[100],k=0,o=-1000,p=-1000,q=-1000;
for (j=0;j<n;j++)
{
s[j]=0;
}
for (int ii=0;ii<m;ii++)
{
while (K+II<M)
{
for (j=0;j<n;j++)
{
S[J]=S[J]+A[K+II][J];
}
for (i=0;i<n;i++)
{
for (j=i;j<n;j++)
{
sum=0;
for (z=i;z<=j;z++)
{
SUM=SUM+S[Z];
}
if (Sum>o) o=sum;
}
if (o>p)
{
P=o;
}
}
k++;
}
if (P>Q)
{
Q=p;
}
k=0;
for (j=0;j<n;j++)
{
s[j]=0;
}
}
cout<< "sub-matrix Max value is" <<q<<endl;
}
Run results
Programming summary
A way to convert the two-dimensional array into a one-dimensional array, so that the problem becomes simple and easy to think, the idea of simplifying the complex is always. When writing a new program, you should be good at using the previously written program, so you need to save the previously written program, not arbitrarily discarded.
Returns the maximum number of sub-arrays in a two-dimensional integer array