Members: Lin Yanlu, Zhang
(This role exchange, I mainly responsible for code review, Code test plan; Zhang is responsible for program analysis, code programming.) )
Topic:
Returns the maximum number of sub-arrays in a two-dimensional integer array
Requirements:
Enter a two-dimensional shaping array with positive numbers in the array and a negative number.
A two-dimensional array is connected to the end of the line, like a belt.
One or more consecutive integers in an array make up a sub-array, each of which has a and.
The maximum value for the and of all sub-arrays.
Ideas:
Based on the combination of one-dimensional annular array and the maximal sub-array of two-dimensional array, we combine two methods to solve the two-dimensional annular array. Suppose the original two-dimensional array a[3][3] is
Create a new two-dimensional array b[3][5] for
1 |
-2 |
3 |
1 |
-2 |
1 |
-3 |
2 |
1 |
-3 |
3 |
-4 |
5 |
3 |
-4 |
Divide it into three sub-arrays, respectively:
Each sub-array, like the previous two-dimensional array, evaluates the maximum subarray of each sub-array, and then compares the last.
Source:
#include <iostream.h> #include <conio.h>int main () {int i,j; int a[3][3]; int b[3][5]; int Jixu; cout<< "This program solves the problem of maximum sub-matrix and the 3*3 matrix of the end-to-end connection" <<endl; cout<< "Please enter each value of the 3*3 matrix" <<endl; for (i=0;i<3;i++) {for (j=0;j<3;j++) {cin>>a[i][j];}} int max=a[0][0]; cout<< "Initial two-dimensional array:" <<endl; for (i=0;i<3;i++) {for (j=0;j<3;j++) {cout<<a[i][j]<< '; } cout<<endl; } for (i=0;i<3;i++) {for (j=0;j<3;j++) {b[i][j]=a[i][j]; B[I][J+3]=A[I][J]; }} cout<< "The array after the end of the connection is:" <<endl; for (i=0;i<3;i++) {for (j=0;j<5;j++) {cout<<b[i][j]<< '; } cout<<endl; } for (i=0;i<1;i++) {b[0][0]=a[0][0]; for (j=0;j<5;j++) {if (a[0][j-1]<0) {b[0][j]=a[0][j]; } else {B[0][J]=B[0][J-1]+A[0][J]; }}} for (i=1;i<3;i++) {for (j=0;j<1;j++) {if (a[i-1][0]<0) {b[i][0]=a[i][0]; } else {b[i][0]=b[i-1][0]+a[i][0]; }}} for (i=1;i<3;i++) {for (j=1;j<5;j++) {if (b[i-1][j-1]<0) {if (b[i-1][j]>=0&&b[i][j-1]>=0) {if (b[i][j-1]>=b[i-1] [j]) {b[i][j]=b[i][j-1]+a[i][j]; } else {b[i][j]=b[i-1][j]+a[i][j]; }} else if (b[i-1][j]>=0&&b[i][j-1]<=0) {b[ I][J]=B[I-1][J]+A[I][J]; } else if (b[i-1][j]<=0&&b[i][j-1]>=0) {b[i][j]=b[i][j-1]+a[i][j]; } else {b[i][j]=a[i][j]; }} else {if (b[i-1][j]>=0&&b[i][j-1]>=0) { B[I][J]=A[I][J]+B[I-1][J]+B[I][J-1]-B[I-1][J-1]; } else if (b[i-1][j]>=0&&b[i][j-1]<=0) {b[i][j]=a[i][j]+b[i -1][J]-B[I-1][J-1]; } else if (b[i-1][j]<=0&&b[i][j-1]>=0) {b[i][j]=a[i][j]+b[i ][J-1]-B[I-1][J-1]; } else {b[i][j]=a[i][j]; }}}} cout<< "To find the maximum sub-matrix and the intermediate process:" <<endl; for (i=0;i<3;i++) {for (j=0;j<5;j++) {cout<<b[i][j]<< ""; } cout<<endl; } cout<<endl; for (i=0;i<3;i++) {for (j=0;j<5;j++) {if (B[i][j]>max) {max =B[I][J]; }}} cout<< "The maximum number of words for a two-dimensional array and is:" <<max+1<<endl; cout<< "Do you want to continue this process (JIXU) 1, continue 0, exit" <<endl; cin>>jixu; if (jixu==1) {cout<<endl;main (); } else {return 0; } getch (); return 0; }
Operation Result:
Summarize:
This time I swapped roles with my little partner, and I was primarily responsible for code reviews. First we determine the direction of the idea and solution, because of the time arrangement, the main body of the code is discussed together, while the loop to find the largest sub-array and other details are written by a person. Later in the review of the Code, it was found that some of the different approaches discussed earlier than the previous two-dimensional array may be due to the fact that our way of thinking differs from mastering the problem of solving.
After testing multiple sets of data, we found some problems and defects, told the partner, and made the perfect changes. With regard to the fixed two-dimensional array of 3*3, this limitation is very large and has not been modified.
After these several classroom exercises, I found that the weakness lies in the determination of ideas. Although there will be a lot of ideas after seeing the problem, but after the proposed to come up with more special circumstances to deny themselves, then listen to the speakers on the podium to share their own ideas (in fact, some of their own similar), and feel really feasible.
The and of the largest sub-arrays in the array (two-dimensional loops)