Title: Returns the and (connectivity) of the largest sub-array in a two-dimensional array
PARTNER: Mencixin Blog Address: http://home.cnblogs.com/u/wanzitou/feed/blog/
Design ideas: Divide the number of rows into a few one-dimensional arrays, for the one-dimensional array, the sum of their largest contiguous array, and record the maximum continuous array of the first and last position, and then determine the maximum continuous array of several one-dimensional array of the position of whether or including (for example, the first row is 1 and 4, the second row is This is connected). Finally, add a positive number that is not included (must be between the first and last position of the largest contiguous array on the previous line). The sum of the outputs before the output is OK.
Code:
#include <iostream>using namespace std; int Max (int n,int a[],int *smark,int *mmark) {int b[100]={0}; int i,sum1=0,max1=0; for (i=0;i<n;i++) {if (sum1<0) {sum1=a[i]; } else {sum1=sum1+a[i]; } b[i]=sum1; } Max1=b[0]; for (i=0;i<n;i++) {if (Max1<b[i]) {max1= b[i]; *mmark = i; }} for (i = *mmark;i >= 0;i--) {if (b[i] = = A[i]) {*smark = i; Break }} return max1;} void Main () {int m,n,i,j,smark,mmark,t2; int Sum,max; int up[100],down[100],t[100]; int a[100][100],b[100]; cout<< "Enter rows and columns of two-dimensional arrays"; cin>>m>>n; for (i=0;i<m;i++) {for (j=0;j<n;j++) {cin>>a[i][j]; }} for (i=0;i<m;i++) {for (j=0;j<n;j++) {b[j]=a[i][j]; } Sum=max (N,b,&sMark,&mmark); Up[i]=smark; Down[i]=mmark; T[i]=sum; } T2=t[0]; for (i=0;i+1<m;i++) {if (up[i]<=down[i+1] && down[i]>=up[i+1]) {t2+=t[i+1]; } for (j=up[i];j<up[i+1];j++) {if (a[i+1][j]>0) t2+=a[i+1][j]; Discriminant independent Positive}} cout<<t2<<endl; }
Results:
Summary: An array to maximize the number of sub-arrays and can evolve so many requirements, we write code should be more flexible to solve, on the basis of the previous year. There are shortcomings that need to be perfected.
Connected maximum subarray and (pair development)