Pair member : Hou Tao Bright: mainly responsible for programming code.
Zhu Shaohui: Mainly responsible for program debugging and modification.
title: A two-dimensional array of the end-to-end, in which there is a positive number, a negative number, to find its maximum sub-matrix.
idea: The solution of this problem is to find the maximal sub-matrix of the two-dimensional array and the largest sub-array and combine with the first dimension. The maximum number of sub-arrays for the solution ring can be divided into two cases. The first type: When the array subscript does not cross the bounds. The second case is that the array is out of bounds.
Code:
#include <iostream>using namespacestd;intMAX (intS[],intN) { inti,sum=0, max=s[0]; for(i=0; i<n;i++) { if(sum>0) {sum=sum+S[i]; } Else{sum=S[i]; } if(sum>max) {Max=sum; } } returnMax;}intMIN (intS[],intN) { inti,sum=0, min=s[0]; for(i=1; i<n;i++) { if(sum<0) {sum=sum+S[i]; } Else{sum=S[i]; } if(sum<min) {min=sum; } } returnmin;}intSUM (intS[],intN) { inti,sum=0; for(i=0; i<n;i++) {sum=sum+S[i]; } returnsum;}voidMain () {intm,n,i,j,a[ -][ -]; cout<<"Please enter the size of the Matrix (m*n):"; CIN>>m>>N; cout<<"Please enter a matrix:"<<Endl; for(i=0; i<m;i++) { for(j=0; j<n;j++) {cin>>A[i][j]; } } intsum,max,s[ -],k=0, min,p=a[0][0]; for(i=0; i<m;i++) { for(j=0; j<n;j++) {S[j]=0; } while(k+i<m) { for(j=0; j<n;j++) {S[j]=s[j]+a[k+i] [j]; } Sum=SUM (s,n); Min=MIN (s,n); Max=MAX (s,n); if(sum-min>max) {Max=sum-min; } if(max>p) {p=Max; } k++; } k=0; } cout<<"the maximum value of the sub-matrix is"<<p<<Endl;}
Summary: The key to solve this problem is to find the maximal sub-matrix of two-dimensional array and the integration of the maximal sub-array with the first dimension and the two problems, we should learn to use the method of function to integrate the program. First understand each module and then assemble each module.
The first two-dimensional array maximal sub-matrix