The sum of the maximum values of two-dimensional array sub-arrays

Source: Internet
Author: User

title : Find the maximum value of the sum of its sub-arrays in a two-dimensional array.

Personnel :

Chen Chen: responsible for program writing.

Wang Yingrui: Responsible for code review and code testing.

idea: for the sum of the sub-arrays of the two-dimensional array, thinking and the difference between one-dimensional array is not much, but need to analyze more cases, I discussed with the roommate after the written. Here's my idea:

1. Determine the maximum upper bound of the subarray, descending from the first row, to specify the range of the largest subarray.

2. For the range of the specified maximum Subarray, the maximum subarray is divided into several different rows according to the number of a column. (If the first group has only one row, the second group has two rows, and the number of columns and columns is the same)

3. For a few arrays produced in the second step, a one-dimensional array is used to find the maximum value of the subarray. The maximum value is calculated in turn, and the maximum

4. Reduce the maximum upper bound by 1, re-2nd, 3, 4 steps.

Code:

#include <iostream>using namespace Std;void main () {int x,y,i,j,m=0,a[100][100]={0};cout<< "input matrix rows and columns"; CIN >>x>>y;if (x>100| | x<0| | y>100| | y<0) {cout<< "input error, please re-enter"; cin>>x>>y;} for (i=0;i<x;i++) {for (j=0;j<y;j++) {cin>>a[i][j];}} int sum[100],max,result=a[0][0];for (i=0;i<x;i++)//determines the maximum upper bound of the Subarray (for line i) {while (m+i<x)//determines that the subarray has a m+i row {// The handle array is the same as an array, the maximum Subarray and for (j=0;j<y;j++) {sum[j]=sum[j]+a[m+i][j];} Max=0;for (j=0;j<y;j++) {if (Max+sum[j]>sum[j]) {max=max+sum[j];} ELSE{MAX=SUM[J];} if (Max>result) {Result=max;}} m++;//is the number of rows of the Subarray +1}//the values of M and sum[], causing the maximum upper bound of the Subarray to fall by 1 and then re-loop. M=0;for (j=0;j<y;j++) {sum[j]=0;}} Cout<<result;}

Work Photo :

Summary : Although this procedure is discussed in our dormitory, but in the process of writing, we also appeared a few different views. But finally, after our consultations and practice, we reached a consensus.

Through this experiment, I understand the importance of teamwork. In the process of reprogramming, we have found some of our own programming aspects (such as the number of inputs in the program to judge, in case of out of scope). After we solved the different opinions between us, I became more aware of the importance of communicating with people.

The sum of the maximum values of two-dimensional array sub-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.