1. Title.
Title: Returns the number of the largest sub-arrays in a two-dimensional integer array.
Requirements:
Enter a two-dimensional shaping array with positive numbers in the array and a negative number.
A two-dimensional array is connected to the end of the line, like a belt.
One or more consecutive integers in an array make up a sub-array, each of which has a and.
The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).
2. Design ideas.
The maximal sub-matrix of each row is calculated separately, and then two lines are added, the maximal sub-matrix is calculated, and the maximal sub-matrix is calculated until all the rows are added. Compare one of the largest sub-matrix values to find the largest.
3. Code.
#include <iostream>using namespacestd; #include<ctime>#include<cstdlib>#defineMAX 10000intMax (intb[]) { intm[max],n=0, p=0; for(intI=0;i<4; i++) { for(intj=0;j<4; j + +) {n=n+b[i+J]; M[P]=N; P++; } N=0; } intmax=m[0]; for(i=0; i<p;i++) { if(m[i]>max) Max=M[i]; } returnMax;}intMain () {intk[max],a[4][8],h[8]; Srand (Time (0)); cout<<"The matrix is:"<<Endl; for(intI=0;i<4; i++) { for(intj=0;j<4; j + +) {A[i][j]=rand ()% -- -; A[i][j+4]=A[i][j]; cout<<a[i][j]<<" "; } cout<<Endl; } for(i=0;i<4; i++) {K[i]=Max (A[i]); } intq=4; for(i=0;i<3; i++) { intt=0; for(intj=0;j<8; j + +) {H[t]=a[i][j]+a[i+1][j]; T++; } K[q]=Max (h); Q++; } for(i=0;i<2; i++) { intt=0; for(intj=0;j<8; j + +) {H[t]=a[i][j]+a[i+1][j]+a[i+2][j]; T++; } K[q]=Max (h); Q++; } intt=0; I=0; for(intj=0;j<8; j + +) {H[t]=a[i][j]+a[i+1][j]+a[i+2][j]+a[i+3][j]; T++; } K[q]=Max (h); Q++; cout<<Endl; intmax=k[0]; for(i=0; i<q;i++) { if(k[i]>max) Max=K[i]; } cout<<"the value of the maximum sub-matrix is:"<<Endl; cout<<Max; return 0;}
4. Run the results.
5. Experience.
With the idea, the programming is much more convenient, even if some aspects of the poor implementation, but we think together, or can solve.
6. Photos.
Returns the and (Kinsoku) of the largest subarray in a two-dimensional integer array