The sum of the maximum number of words in a two-dimensional array

Source: Internet
Author: User

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. 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.

(1) Code:

#include <iostream>
UsingNamespaceStd
#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;
Forint 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
}
Elseif ((x==0) && (y== (n1)))
{
sum=a[0];b=a[0];x=0;y=0;
Forint o=1;o<n;o++)
{
if (a[o]>Sum
{sum=a[o];x=o;y=o;}
}
s=sum;jmin=x;jmax=Y
}
}

IntMain ()
{
IntA[n][n];
Forint i=0;i<n;i++)
{
Forint j=0;j<n;j++)
{
A[i][j]=rand ()%6;
if (rand ()%2==1)
a[i][j]=a[i][j]* (-1);
}
}
cout<<"A randomly generated two-dimensional array is:"<<endl<<Endl
Forint i=0;i<n;i++)
{
Forint 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;
Forint i=0;i<n;i++)
{
Forint j=i;j<n;j++)
{
int b[n]={0};////////////////define an array that can hold the sum of rows
Forint k=0;k<n;k++)
{
Forint 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;
   }
}

The sum of the maximum number of words for a two-dimensional array with end-to-end connections

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.