The first two-dimensional array maximal sub-matrix

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.