First, the topic requirements
Title: Returns the number of the largest sub-arrays in a two-dimensional integer array. Requirements: Enter a two-dimensional shape array with positive and negative numbers in the array. 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. Requires a time complexity of O (n). Second, the design idea first calls the Maxsubarray function of the sum of the two-dimensional maximal continuous sub-arrays to find the sum of the two-dimensional maximal continuous subarray, and then the sum of the maximal contiguous subarray can be obtained by moving the elements in column K to the left one column. Cycle m (because the original two-dimensional array has m-columns) each maximum value is saved in an arr one-dimensional array, and finally the maximum value of the ARR array is the sum of the maximum contiguous subarray of the two-dimensional array that the topic is seeking. Third, the Code
#include <iostream.h>int maxsubarray (int **a,int n,int m) {int **p=new int*[n];int i,j;if (m==0| | n==0) return 0;//calculation p[i][j]for (i=0;i<n;i++) {p[i]=new int[m];for (j=0;j<m;j++) {if (i==0) {if (j==0) p[i][j]=a[i][j ];ELSEP[I][J]=P[I][J-1]+A[I][J];} Else{if (j==0) p[i][j]=p[i-1][j]+a[i][j];elsep[i][j]=p[i][j-1]+p[i-1][j]-p[i-1][j-1]+a[i][j];}} Computes the maximum subarray of two-dimensional arrays and int temp;int max=a[0][0];int sum;//if M==1if (m==1) {for (i=0;i<n;i++) {for (j=i;j<n;j++) {if (i==0) {temp=p[j][m-1];} ELSE{TEMP=P[J][M-1]-P[I-1][M-1];} if (sum<temp) sum=temp;}}} Else{for (i=0;i<n;i++) {for (j=i;j<n;j++) {if (i==0) {temp=p[j][m-1]-p[j][m-2];} Else{temp=p[j][m-1]-p[j][m-2]-p[i-1][m-1]+p[i-1][m-2];} for (int k=m-2;k>=0;k--) {if (temp<0) temp=0;if (i==0) {if (k==0) temp+=p[j][k];elsetemp+=p[j][k]-p[j][k-1];} Else{if (k==0) temp+=p[j][k]-p[i-1][k];elsetemp+=p[j][k]-p[j][k-1]-p[i-1][k]+p[i-1][k-1];} if (sum<temp) sum=temp;}}}} return sum;} int main () {int n,m;int max;cout<< "Please enter the number of rows for the two-dimensional array:" <<endl;cin>>n;cout<< "Enter the columns of the two-dimensional arrayNumber "<<endl;cin>>m;int i,j;int **a=new int*[n];int *arr=new int[m];cout<<" Please enter this two-dimensional array element: "<<endl; for (i=0;i<n;i++) {a[i]=new int[m];for (j=0;j<m;j++) {cin>>a[i][j];}} for (i=0;i<m;i++) {Arr[i]=maxsubarray (a,n,m), for (int. k=0;k<n;k++) {for (j=0;j<m;j++) {a[k][(j+1)%m]=a[k][j] ;} Loop moves the elements of the K column left one column}}for (i=0;i<m;i++) {max=arr[0];if (Max<arr[i]) {max=arr[i];}} cout<< "The sum of the largest contiguous subarray of two-dimensional arrays:" <<max<<endl;return 0;}
Iv. Testing
V. Summary
I feel like I'm in the right place. But there was an error in the implementation, and I tried to find the error in the code and changed the main function to output the sum of the results of each maximal subarray.
So I guess it's a problem with this part of the code.
for (i=0;i<m;i++) {Arr[i]=maxsubarray (a,n,m), for (int. k=0;k<n;k++) {for (j=0;j<m;j++) {a[k][(j+1)%m]=a[k][j] ;} Loop moves the elements of the K column left one column}}
But I feel very heli logical thinking (drunk, play these two words appear), do not know where the wrong, get a long time, tangled in!
Six, work photo
The development of software engineering pair The sum of 2 consecutive maximal sub-arrays in a two-dimensional array