There is still a problem with the last circular two-dimensional array, when it was not judged whether the resulting sub-matrices exceeded the range of the matrix, so the result would be problematic.
This is judged in the improved version of the program.
This problem is divided into two types, one is the maximum number of sub-rings, one is the largest sub-array is not ring. The part that is not in the loop has been implemented before, and the following is the ring part.
The idea is to expand the matrix to resemble a one-dimensional array, but the extent of the sub-matrix may go beyond the matrix if it expands. So there's a judgment here.
The improved program
#include <iostream>#include<time.h>using namespacestd;#defineMax (a) (a) > (b)? ( A):(B))#defineMAXN 100intA[MAXN][MAXN];intPARTSUM[MAXN][MAXN];//computes the and of the sub-matricesintMatrixsum (intSintTintIintj) { returnPARTSUM[I][J]-partsum[i][t-1]-Partsum[s-1][J] + partsum[s-1][t-1];}intMain () {srand (unsigned) time (NULL)); intRow, col, I, J; cout<<"Please enter the number of rows and columns of the two-dimensional array:"; CIN>> Row >>Col; for(i =1; I <= row; i++) { for(j =1; J <=2* Col-1; J + +) { for(j =1; J <=col; J + +) {A[i][j]= rand ()% --Ten; cout<< A[i][j] <<"\ t"; } for(j = col+1; J <=2* Col-1; J + +) {A[i][j]= A[i][j-Col]; cout<< A[i][j] <<"\ t"; } //cout << a[i][j] << "";} cout<<Endl; } for(i =0; I <= row; i++) partsum[i][0] =0; for(j =0; J <=2* col-1; J + +) partsum[0][J] =0; //calculate the part of the Matrix and for(i =1; I <= row; i++) for(j =1; J <= Col; J + +) Partsum[i][j]= A[i][j] + partsum[i-1][J] + partsum[i][j-1]-Partsum[i-1][j-1]; intN1, N2; intMaxsofar = a[1][1]; for(N1 =1; N1 <= Row; n1++) for(N2 = n1; N2 <= Row; n2++) { //Set the sub-matrix upper and lower bounds to line N1 and line N2, take the maximum value in these sub-matrices, similar to the one-dimensional array to find the maximum value//not formed ring intMaxendinghere = Matrixsum (N1,1, N2,1); for(j =2; J <= col-1; J + +) {Maxendinghere= Max (Matrixsum (N1, J, N2, J), Matrixsum (N1, J, N2, J) +maxendinghere); Maxsofar=Max (Maxendinghere, Maxsofar); } //Looping intsum = matrixsum (n1,1, N2,1); intStart =sum; intsind =1; for(i =2; I <=row; i++) {sum+=Matrixsum (N1, I, N2, i); if(Sum >start) {Start=sum; Sind=i; }} maxendinghere=Matrixsum (N1, J, N2, J); intTind =Row; for(j = row-1; J >=1; j--) {sum+=Matrixsum (N1, J, N2, J); if(Sum >maxendinghere) {Maxendinghere=sum; Tind=J; } } if(sind<tind && start + maxendinghere>Maxsofar) {Maxsofar= Start +Maxendinghere; }} cout<<Maxsofar;}
Annular two-dimensional array (improved version)