Returns the maximum number of sub-arrays in a two-dimensional integer array

Source: Internet
Author: User

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

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.