Pair development--the problem of finding the maximal and maximum of all sub-matrices of a two-dimensional annular array

Source: Internet
Author: User

First, the topic requirements:

Enter a two-dimensional shaping array with positive numbers in the array and a negative number.
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) Topic: Returns the number of the largest subarray in a two-dimensional integer array.

Second, the solution of ideas:

Since the last time we did the problem of finding the maximal sub-matrix of the two-dimensional array and the problem of finding the maximal value of the sub-array of the one-dimensional annular array, this time, we have modified the two-dimensional array first, then we have reconstructed the second-dimension arrays, and then we have obtained the final result by the method of finding the maximal

Third, the program code:

#include"stdafx.h"#include<iostream.h>intMainintargcChar*argv[]) {    inti,j; inta[3][5]={{1,-2,3},{1,-3,2},{4,-4,5}}; intb[3][5];  for(i=0;i<3; i++)    {      for(j=0;j<2; j + +) A[i][j+3]=A[i][j]; }    intmax=a[0][0]; cout<<"the initial two-dimensional array is:"<<Endl;  for(i=0;i<3; i++)    {         for(j=0;j<3; j + +) {cout<<a[i][j]<<' '; } cout<<Endl; } cout<<"the reconstructed ring array is:"<<Endl;  for(i=0;i<3; i++)    {         for(j=0;j<5; j + +) {cout<<a[i][j]<<' '; } cout<<Endl; }     for(i=0;i<1; i++) {b[0][0]=a[0][0];  for(j=0;j<5; j + +)        {            if(a[0][j-1]<0) {b[0][j]=a[0][j]; }            Else{b[0][j]=b[0][j-1]+a[0][j]; }                }    }     for(i=1;i<3; i++)    {         for(j=0;j<1; j + +)        {            if(a[i-1][0]<0) {b[i][0]=a[i][0]; }            Else{b[i][0]=b[i-1][0]+a[i][0]; }        }    }     for(i=1;i<3; i++)    {         for(j=1;j<5; j + +)        {            if(b[i-1][j-1]<0)            {                if(b[i-1][j]>=0&&b[i][j-1]>=0)                {                    if(b[i][j-1]>=b[i-1][j]) {B[i][j]=b[i][j-1]+A[i][j]; }                    Else{B[i][j]=b[i-1][j]+A[i][j]; }                }                Else if(b[i-1][j]>=0&&b[i][j-1]<=0) {B[i][j]=b[i-1][j]+A[i][j]; }                Else if(b[i-1][j]<=0&&b[i][j-1]>=0) {B[i][j]=b[i][j-1]+A[i][j]; }                Else{B[i][j]=A[i][j]; }            }            Else            {                if(b[i-1][j]>=0&&b[i][j-1]>=0) {B[i][j]=a[i][j]+b[i-1][j]+b[i][j-1]-b[i-1][j-1]; }                Else if(b[i-1][j]>=0&&b[i][j-1]<=0) {B[i][j]=a[i][j]+b[i-1][j]-b[i-1][j-1]; }                Else if(b[i-1][j]<=0&&b[i][j-1]>=0) {B[i][j]=a[i][j]+b[i][j-1]-b[i-1][j-1]; }                Else{B[i][j]=A[i][j]; } }}} cout<<"the arrays of the sub-matrices are:"<<Endl;  for(i=0;i<3; i++)    {         for(j=0;j<5; j + +) {cout<<b[i][j]<<" "; } cout<<Endl; } cout<<Endl;  for(i=0;i<3; i++)    {         for(j=0;j<5; j + +)        {            if(b[i][j]>max) Max=B[i][j]; }} cout<<"Maximum sub-matrices and is:"<<max<<Endl; return 0; }

Iv. Results

We set up an easy-to-observe array, which is completely different from the original, and the maximal sub-matrix is also at a glance.

Five, Experience:

Because before we have the foundation, so this feeling is relatively simple, both in the idea or in the programming process are feeling more smooth, so spend time is not too much, just refactor the number of groups, and then simply modified a few places.
In this process, I and xueqing especially feel good programming habits and clear solution is very important, whether for code rewriting, or new function module, can quickly improve the efficiency of programming.

Pair development--the problem of finding the maximal and maximum of all sub-matrices of a two-dimensional annular array

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.