I. Topics and Requirements
Title, returns the and of the largest subarray in a two-dimensional integer array
requirements, 1, input a two-dimensional shaping array, there are positive numbers in the array and negative numbers.
2, two-dimensional array of the end-to-end phase, like a first-and-last tape.
3, a contiguous array of one or more integers constitute a sub-array, each sub-array has a and
Second, design ideas
On the basis of the last thought of the loop array, and the two-dimensional array to find the maximum number of sub-arrays combined, the function is merged to complete the topic requirements.
- The first step, the array of each row as the loop array to find the largest sub-array
- The second step, by enumerating each case and depositing it into a new two-dimensional array
- The third, by the method of calculating the maximum sub-array by column, the largest and most of all sub-matrices
such as: The original two-dimensional array
After each line is summed,
Sum by column,
| 1 |
1+2 |
2 |
2+1 |
| 1+3 |
1+2+3+4 |
2+4 |
2+1+4+3 |
| 3 |
3+4 |
4 |
4+3 |
So we can find out all the sub-matrices and then compare the sizes.
Third, the source code
1 //ketang5.cpp: Defines the entry point of the console application. 2 //Zhang Shitong Liang3 4#include"stdafx.h"5#include"iostream"6 using namespacestd;7 8 /*two-dimensional array compression function*/9 voidYasuoint**source,int**destion,intRowintLine )Ten{//The array elements of each row are evaluated using the enumeration method for the sub-array One intI,j,add,count; A for(i=0; i<row;i++) - { -Count=0; the for(j=0; j<line;j++) - { -Add=0; - for(intL=0; l<line;l++)//each row is enumerated sequentially in a stored array + { -add=add+source[i][l+j]; +destion[i][count+l]=add; A } atcount=count+Line ; -source[i][line+j]=Source[i][j]; - } - } - } - in /*To find the maximum value function*/ - intMax (int**destion,intRowintLine ) to { + intI,j,add,max; -max=destion[0][0];//assigns the first number of arrays to Max the for(j=0; j<line;j++) * { $ for(i=0; i<row;i++)Panax Notoginseng { -Add=0; the for(intR=0; r<row-i;r++) + { AADD=ADD+DESTION[R+I][J];//enumerate by column array to find the maximum value the if(max<add) + { -max=add; $ } $ } - } - } the returnMax; - }Wuyi the /*Array Initialization*/ - voidChushihua (int**arr,intRowintLine ) Wu { -cout<<"Please enter array data"<<Endl; About for(intI=0; i<row;i++) $ { - for(intj=0; j<line;j++) - { -Cin>>Arr[i][j]; A } + } the } - $ intMain () the { the Chars; the intRow,col1,col2;//define the number of row series thecout<<"Please enter the number of lines:"; -Cin>>Row; incout<<"Please enter the number of columns:"; theCin>>col1; the int**a;//dynamically defining two-dimensional arrays About int**B; theCol2=col1*col1; theA=New int*[row]; the for(intk=0; k<row;k++) + { -a[k]=New int[col1]; the }Bayib=New int*[row]; the for(intL=0; l<row;l++) the { -b[l]=New int[col2]; - } the Chushihua (a,row,col1); the Yasuo (a,b,row,col1); the intmax=Max (b,row,col2); thecout<<"The sum of the largest sub-arrays is:"<<max<<Endl; -cout<<Endl; thecout<<"whether to continue (y/n)"; theCin>>s; the while(s!='Y'&&s!='y'&&s!='N'&&s!='N')94 { thecout<<"input Error, please re-enter (y/n)"; theCin>>s; the }98 if(s=='Y'|| s=='y') About { - main ();101 }102 return 0;103}
Four. Running
Five. Summary experience
1. This is my last cooperation with worldcom in the development of soft knot, he has been very positive attitude to promote cooperation between us, a lot of ideas are very useful, in short, he is a good partner, in the future I would like to make progress with him.
2. Our procedure is basically to follow the basic idea of the previous, with the experience of the previous several times, it is felt that the idea of this procedure is like yielded in the mind, and then the implementation is quite smooth.
Six. Group photo
Pair--two-D array to find the maximum subarray and