The end-to-end of a two-dimensional array of maximal sub-arrays

Source: Internet
Author: User

#include <iostream>

using namespace Std;

#define N 4

void Maxhe (int a[],int &s,int &jmin,int &jmax)

{

int sum=a[0],b=a[0],x=0,y=0;

int j=1;

while ((j-x<n) &&x<n)

{

if (b>=0)

{

B=a[j%n];

if (j<n)

X=j;

Else

Break

}

Else

B+=a[j%n];

if (sum>=b)

{sum=b;y=j;}

j + +;

}

sum=0;

for (int i=y+1;i<x+n;i++)

Sum+=a[i%n];

s=sum;jmin=y+1;jmax=x+n-1;

int z=y%n;

if (x>=z)

{

j=1;sum=a[0];b=a[0];x=0;y=0;

while (J<n)

{

if (b<0)

{b=a[j%n];x=j;}

Else

B+=a[j%n];

if (sum<=b)

{sum=b;y=j;}

j + +;

}

S=sum;jmin=x;jmax=y;

}

else if ((x==0) && (y== (N-1)))

{

sum=a[0];b=a[0];x=0;y=0;

for (int o=1;o<n;o++)

{

if (a[o]>sum)

{sum=a[o];x=o;y=o;}

}

S=sum;jmin=x;jmax=y;

}

}

int main ()

{

int a[n][n];

for (int i=0;i<n;i++)

{

for (int j=0;j<n;j++)

{

A[i][j]=rand ()%6;

if (rand ()%2==1)

a[i][j]=a[i][j]* (-1);

}

}

cout<< "randomly generated two-dimensional arrays are:" <<endl<<endl;

for (int i=0;i<n;i++)

{

for (int j=0;j<n;j++)

{

cout<<a[i][j]<< "";

}

cout<<endl;

}///////////////////////////////////////////////two-dimensional array generation

int max=a[0][0],c1=0,c2=0,l1=0,l2=0;

for (int i=0;i<n;i++)

{

for (int j=i;j<n;j++)

{

int b[n]={0};////////////////defines an array that can hold the sum of rows

for (int k=0;k<n;k++)

{

for (int l=i;l<=j;l++)

{

B[K]+=A[L][K];

}

cout<<b[k]<< "";

int s=b[0],jmin=0,jmax=0;

Maxhe (B,s,jmin,jmax);

if (S>=max)

{max=s;c1=jmin;c2=jmax;l1=i;l2=j;}

}

cout<<endl;

}

}

cout<< "Two-dimensional array maximum and:" <<max<<endl;

for (int i=l1;i<=l2;i++)

{

for (int j=c1;j<=c2;j++)

{

cout<<a[i][j%n]<< "";

}

cout<<endl;

}

}

Members: Song Yujia Zhou Xieying

Idea: Starting from the first line, plus a row plus two lines, all the two-dimensional arrays have been converted into several one-dimensional arrays, and then the last one-dimensional array of the first-to-most number of words combined into a function call.

Feel: Every time she comes to programming, next time I must make up, whether can do it, always try.

The end-to-end of a two-dimensional array of maximal sub-arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.