Design ideas:
First of all must be the two-dimensional conversion to a one-dimensional array to compare, so that first starting from the first row, as a one-dimensional array to select the largest sub-array of the specific implementation is, set the sub-array and for sum=0, another b=0,a[0][i] from a[0][0] start to retrieve when B<0 B=a[0][i] Otherwise b=b+a[0][i] only when B>sum sum=b, (first to ensure that the first assignment of sum is greater than 0 and then is the operation of the first occurrence of a positive number B is assigned to B at this time sum=0,b>sum, so sum=b, Then proceed to the last step of the positive and negative judgment of B decomposition for, starting from the previous step b>0 so b=b+a[0][i], if the a[0][i]>0,b increase sum<b,sum=b, if the a[0][i]<0,b to reduce the sum=b do not execute, The next step is how to execute the result of looking at the B results so that the array is kept accumulating the current array greater than 0 can be continued and the subsequent sub-array is added because a larger sub-array containing it may also appear, if B is less than 0 it would be cumbersome to discard the sub-array directly after the new start Addend Group), The second step is to add the first row and the second line to the second row of the new array, and then repeat the previous step, unlike the sum of the largest subarray of the first row at this time, so that it is added to the last line and then starts from the second line and then starts from the third line until the last row to find the sum of the largest subarray
Implementation code:
#include <iostream>
using namespace Std;
void Max (int row,int col) {
int * * A=new int*[row];
int * * B=new int*[row];
for (int i=1;i<=row;i++) {
A[i]=new Int[col];
}
for (int i=1;i<=row;i++) {
B[i]=new Int[col];
}
cout<< "Enter" <<row<< "Rows" <<col<< "elements of an array of columns" <<endl;
int count=0;
for (int i=1;i<=row;i++) {
for (int j=1;j<=col;j++) {
cin>>a[i][j];
B[I][J]=A[I][J];
if (a[i][j]<0) {
count++;
}
}
}
cout<< "The array entered is:" <<endl;
for (int i=1;i<=row;i++) {
for (int j=1;j<=col;j++) {
cout<<a[i][j]<< "";
}
cout<<endl;
}
int k,n=2;
if (Count<row*col) {
int sum=0;
for (int m=1;m<=row;m++) {
K=n;
for (int i=1;i<=row;i++) {
for (int j=1;j<=col;j++) {
B[I][J]=A[I][J];
}
}
for (int i=m;i<=row;i++) {
while (k<=i) {
for (int j=1;j<=col;j++) {
B[K][J]=B[K-1][J]+B[K][J];
cout<<b[k][j]<< "";
}
k++;
}
}
n++;
for (int i=m;i<=row;i++) {
int c=0;
for (int j=1;j<=col;j++) {
if (c<0) {
C=B[I][J];
}
else {
C=C+B[I][J];
}
if (sum<c) {
Sum=c;
}
}
}
}
cout<<endl;
cout<< "The number of the largest sub-arrays is:" <<sum;
}
else {
int sum=a[1][1];
for (int i=1;i<=row;i++) {
for (int j=1;j<=col;j++) {
if (a[i][j]>sum) {
SUM=A[I][J];
}
}
}
cout<<endl;
cout<< "The number of the largest sub-arrays is:" <<sum;
}
for (int i=1;i<=row;i++) {
delete []a[i];
A[i]=null;
}
for (int i=1;i<=row;i++) {
delete []b[i];
B[i]=null;
}
}
void Main () {
int row,col;//row and column number
cout<< "Please enter the number of rows and columns of the array:";
cin>>row>>col;
Max (Row,col);
}
Results:
Summary of Experience:
The first is the failure to achieve the time complexity is O (n) requirements, design ideas are very important, with the idea can determine whether they can be done by code and how to complete, see the topic to have the overall solution of the idea, and on this basis, and constantly improve the idea, Rather than the idea of the world, but not the realization or implementation, summed up from the problem of the solution to the idea of the method, the first should be until how to solve with the most stupid method is a calculation, and then the complex simplification such as two-dimensional conversion to one-dimensional, the regularity of the use of variables.
Returns a two-dimensional array of the largest matrices and