To find the largest subarray of two-dimensional arrays

Source: Internet
Author: User

Pair Development member: Zhu Shaohui, Hou Tao Bright

Zhu Shaohui: Responsible for program analysis, code programming

Hou Tao: Responsible for code review and Code testing

Design ideas:

According to the largest subarray of the one-dimensional subarray and the example, the two-dimensional array into a number of one-dimensional sub-arrays, such as M-row N-column array can be divided into (m+1) *M/2 Sub-array, in the order of each one-dimensional array of the largest sub-array, and put these and into an array, The maximum value of this array is the same as the maximum subarray of the two-dimensional array. The time complexity of this algorithm is O (n^3).

Code:

#include <iostream>using namespacestd;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=a[0][0],s[ -],k=0;  for(i=0; i<m;i++)    {            while(k+i<m) { for(j=0; j<n;j++) {S[j]=s[j]+a[k+i]                [j]; } Sum=0;  for(j=0; j<n;j++)                {                       if(s[j]+sum>S[j]) {Sum=s[j]+sum; }                       Else{sum=S[j]; }                       if(sum>max) {Max=sum; }} k++; } k=0;  for(j=0; j<n;j++) {S[j]=0; }} cout<<"the maximum value of the sub-matrix is"<<max<<Endl;}

Summarize:

This programming I mainly responsible for the program analysis, code programming, which has greatly improved my programming ability. One-dimensional arrays and two-dimensional arrays to find the maximum number of sub-arrays and the idea is similar, visible programs can be categorized. Through the cooperation we completed the programming and testing, felt the importance of cooperation and division of Labor, but also learned how to communicate when there are differences, to persuade each other, the harvest is great.

To find the largest subarray of two-dimensional arrays

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.