Pair member: Van der Zhao Yonghen
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
Two. 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
Three. Source code
#include "stdafx.h" #include "iostream" #include <vector> using namespace std; const int N = 101; int a[n][n], p[n][n],b[n][n]; int maxrecsum (int n) {for (int i = 0; I <= N; ++i) {p[i][0] = 0; P[0][i] = 0; } for (int i = 1, i <= N; ++i) {for (int j = 1; j <= N; ++j) p[i][j] = p[i-1][j] + P I [J-1]-p[i-1][j-1] +a[i][j]; } int max = Int_min; for (int i = 1, i <= N; ++i) {for (int j = i; j <= N; ++j) {int sum = 0; for (int k = 1; k <= N; ++k) {int temp = P[j][k]-p[j][k-1]-p[i-1][k] + p[i-1][k-1] ; if (sum > 0) sum + = temp; else sum = temp; if (Sum > max) max = sum; }}}} return max; } int main () {int n = 3; int num; cout<< "Matrix with a size of three rows and three columns, enter a value:" <<endl; for (int i = 1, i <= N; ++i) {for (int j = 1; j <= N; ++j) {cin >> num; A[I][J] = num; }} int b[3][6]; for (int t=0;t<3;t++) {b[t][0]=a[t][0]; B[T][1]=A[T][1]; B[T][2]=A[T][2]; B[T][3]=B[T][0]; B[T][4]=B[T][1]; B[T][5]=B[T][2]; } cout << "Maximum matrix of the and is:" << maxrecsum (n) << Endl; for (int i = 1, i <= N; ++i) {for (int j = 1; j <= N; ++j) {cout <<A[I][J]&L t;< ""; } cout<<endl; } return 0; }
Four. Running
Five. Experimental impressions
The implementation of this experiment, time is a little tight, so there are many shortcomings in the function, the optimization of the program is enough to improve, I believe that there is more time, we can do better.
Six. Pair member group photo
Pair Development 6----end-to-end matrix