1, topic Requirements
According to the introduction of software engineering--"ring one-dimensional array maximum subarray and" and "two-dimensional array maximum subarray and" two blog, the two-dimensional ring-shaped array.
2, thinking Design
According to the previous two blog ideas to do reference. On the basis of two-dimensional array, we extend the column of the two-dimensional array 2*col-1. Then the sum of the two-dimensional arrays can be done.
3, Code
#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 + +) {cin>>A[i][j]; } for(j = col +1; J <=2* Col-1; J + +) {A[i][j]= a[i][j-Col]; } } } for(i =0; I <= row; i++) partsum[i][0] =0; for(j =0; J <= Col; J + +) partsum[0][J] =0; //calculate the part of the Matrix and for(i =1; I <= row; i++) for(j =1; J <=2*col-1; 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 intMaxendinghere = Matrixsum (N1,1, N2,1); for(j =2; J <=2* col-1; J + +) {Maxendinghere= Max (Matrixsum (N1, J, N2, J), Matrixsum (N1, J, N2, J) +maxendinghere); Maxsofar=Max (Maxendinghere, Maxsofar); }} cout<<Maxsofar;}
4, results
5, summary
The technical is not high, is to change the number of columns of the two-dimensional array. The emphasis is on reusing the previous program. From one-dimensional arrays to two-dimensional arrays, one-dimensional ring-shaped arrays to ring-shaped two-dimensional arrays. Each step of the update has a previous program of the idea of the shadow to pave the way. The reuse and change of code is very simple and applicable.
Introduction to Software engineering---the largest subarray of annular two-dimensional arrays and