Design idea: The idea that the maximum subarray is obtained by combining the previous dimension and the two-dimensional array.
Screen:
:
Summarize:
The program is encapsulated into a set of interfaces, when the use of the direct call can be.
Source:
//Jiangzhongxi 2015/4/16
#include <iostream>
usingnamespaceStd
voidMaxintRowintCol
{
int* * a=Newint*[row+1];
int* * b=Newint*[row+1];
intH//the number of added elements on each one-dimensional array is sufficient for one ring to begin the next
for(intI=1; i<=row;i++)
{
a[i]=Newint[2*col];
}
for(intI=1; i<=row;i++)
{
b[i]=Newint[2*col];
}
cout<<"Please enter"<<row<<"Line"<<col<<"elements of an array of columns"<<endl;
intCount=0;
for(intI=1; i<=row;i++)
{
for(intj=1; j<=col;j++)
{
cin>>a[i][j];
B[I][J]=A[I][J];
if(a[i][j]<0)
{
count++;
}
}
}
for(intI=1; i<=row;i++)//doubles the two-dimensional array looping
{
for(intj=col+1; j<=2*col-1; j + +)
{
A[i][j]=a[i][j-col];
}
}
/*cout<< "The array entered is:" <<endl;
for (int i=1;i<=row;i++)
{
for (int j=1;j<=2*col-1;j++)
{
cout<<a[i][j]<< "";
}
cout<<endl;
}*/
intk,n=2;
if(Count<row*col)//that is, not all elements are negative.
{
intsum=0;
for(intm=1; m<=row;m++)
{
K=n;
for(intI=1; i<=row;i++)
{
for(intj=1; j<=2*col-1; j + +)
{
B[I][J]=A[I][J];
}
}//B[i] is a meta-calculated array and a[i] is used to preserve the original array
for(inti=m;i<=row;i++)
{
while(k<=i)//two lines are added only when the number of rows is equal to 2 o'clock
{
for(intj=1; j<=2*col-1; j + +)
{
b[k][j]=b[k-1][J]+B[K][J];
}
k++;
}
}
/*cout<< "B" <<endl;
for (int i=1;i<=row;i++)
{
for (int j=1;j<=2*col-1;j++)
{
cout<<b[i][j]<< "";
}
cout<<endl;
}*/
n++;
for(inti=m;i<=row;i++)
{
intC=0;
H=1;
while(H<=col)
{
for(intj=h;j<=col-1+h;j++)
{
if(c<0)
{
C=B[I][J];
}
Else
{
C=C+B[I][J];
}
if(SUM<C)
{
Sum=c;
}
//cout<<sum<< "";
}
h++;
C=0;//lap End C-clear 0 sum hold
}
}
}
cout<<endl;
cout<<"the number of the largest sub-arrays is:"<<sum;
}
Else
{
intsum=a[1][1];
for(intI=1; i<=row;i++)
{
for(intj=1; j<=col;j++)
{
if(a[i][j]>sum)
{
SUM=A[I][J];
}
}
}
cout<<endl;
cout<<"the number of the largest sub-arrays is:"<<sum;
}
for(intI=1; i<=row+1; i++)//To delete a dynamic array
{
Delete[]a[i];
A[i]=null;
}
for(intI=1; i<=row+1; i++)//To delete a dynamic array
{
Delete[]b[i];
B[i]=null;
}
}
voidMain ()
{
intRow,col;//number of rows and columns
cout<<"Please enter the number of rows and columns of the array:";
cin>>row>>col;
Max (Row,col);
}
The two-dimensional number of the largest and sub-arrays of the rings and