Pair development ⅵ--Loop two-dimensional array summation of the largest sub-array

Source: Internet
Author: User

I. Topics and Requirements

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. Requires a time complexity of O (n).

Two. Design ideas

(1) The two-dimensional array we use is the form of arrays;

(2) Read the two-dimensional array of NXM, store it into nx2m, and repeat the former M-column to the last of the two-dimensional array;

(3) The method of searching for the largest array is the same as the non-cyclic two-dimensional array, which is then limited by the judging conditions.

Third, the source code

1 //A two-dimensional array loop. Cpp:defines The entry point for the console application.2 //3 4#include"stdafx.h"5#include"fstream.h"6#include"iostream.h"7 #defineMAXSIZE 508  9 //* * * * Read array information * * *Ten voidReadarr (intArr[][maxsize],int&len1,int&len2) One { AIfstream infile ("Arr.txt"); -     if(!infile) -cout<<"Read failed! "<<Endl; the     Else -     { -Infile>>len1>>Len2; -          for(intI=0; i<len1;i++) +         { -              for(intj=0; j<len2;j++) +             { Ainfile>>arr[i][j];//reading data from a file atarr[i][j+len2]=Arr[i][j]; -             } -         } -     } -  - } in //* * * * Display MATRIX * * * - voidShowarr (intArr[][maxsize],intLen1,intLen2,intSize1,intsize2) to { +      for(inti=len1;i<=size1;i++)//to output an array from a file -     { the          for(intj=len2;j<=size2;j++) *         { $cout<<arr[i][j]<<"\ t";Panax Notoginseng         } -cout<<Endl; the     } + } A //* * * * Summation formula * * * the intGetsum (intArr[][maxsize],intLen1,intLen2,intSize1,intsize2) + { -     intsum=0; $      for(inti=len1;i<=size1;i++)//The number of two numbers in an arbitrary array . $     { -          for(intj=len2;j<=size2;j++) -         { thesum+=Arr[i][j]; -         }Wuyi     } the     returnsum; - } Wu   - intMainintargcChar*argv[]) About { $     intLen1,len2,max,sum;//Len1 is the number of rows, Len2 is the number of columns -     intLine1,line2,row1,row2;//and the two coordinates of the largest matrix -     intArr[maxsize][maxsize]; - Readarr (arr,len1,len2); Acout<<"Matrix:"<<Endl; +Showarr (arr,0,0, len1-1, len2-1); thecout<<Endl; -line1=0; $Line2=0; therow1=0; theRow2=0; thesum=0; themax=arr[0][0]; -      for(intI=0; i<len1;i++)//number of rows in the first number in     { the          for(intj=0; j<len2;j++)//number of columns in the first number the         { About              for(intm=i;m<len1;m++)//number of rows in the second number the             { the                  for(intN=j; (n<2*LEN2) && (n<j+3); n++) the{//number of columns for the second number +Sum=getsum (Arr,i,j,m,n);//Find out the matrix of these two numbers and -                     if(sum>max) the                     {Bayimax=sum; theLine1=i;//Save the first number of rows theLine2=m;//save a row for the second number -Row1=j;//Save the first number of columns -Row2=n;//Save column for second number the                     } the                 } the             } the         } -     } thecout<<"and the largest sub-matrix:"<<Endl; the Showarr (arr,line1,row1,line2,row2); thecout<<"the largest and:"<<max<<Endl;94     return 0; the}

Four, the operation

Five, experience

This expansion we soon realized, because when we write two-dimensional arrays, our thinking is clearer, function function is more exclusive specification, so in the expansion of the time is easier. The idea that we used the last one-dimensional cycle number group of students, is to write the data at the end of each row of the row, and the length of the traversal with the restrictions on the condition can be.

This expansion and the last time the formation of a strong contrast, or good algorithm clear thinking is better. Because our one-dimensional array algorithm didn't write very well last time, we had to rewrite it in the later expansion.

Six, no picture no truth

Pair development ⅵ--Loop two-dimensional array summation of the largest sub-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.