Returns the maximum number of sub-arrays in an array of integers and 4

Source: Internet
Author: User

Title: Returns the number of the largest sub-arrays in a two-dimensional integer array. Requirements: 1 Enter a two-dimensional shaping array with positive numbers in the array and negative values. A 22-D array is connected to the end of the line, like a belt. 3 contiguous one or more integers in an array make up a sub-array, each of which has a and. 4 The maximum value of the and of all sub-arrays is evaluated. Requires a time complexity of O (n). Design ideas The current solution is the most stupid method, poor lifting, so the complexity of time does not reach the requirements of the topic, but also need to further find the answer source code title: Returns the maximum number of sub-arrays in a two-dimensional integer array. Requirements: 1 Enter a two-dimensional shaping array with positive numbers in the array and negative values. A 22-D array is connected to the end of the line, like a belt. 3 contiguous one or more integers in an array make up a sub-array, each of which has a and. 4 The maximum value of the and of all sub-arrays is evaluated. Requires a time complexity of O (n). Design ideas The current solution is the most stupid method, poor lifting, so the complexity of time does not reach the requirements of the topic, but also need to further find the answer source code
#include <iostream>#include<time.h>#include<conio.h>#include<fstream>using namespaceStd;ofstream out;voidRandin (intRowintnum,intColintnum,inta[][ -])//randomly generate a two-dimensional array{     for(intI=0; i<rowintnum;i++)    {         for(intj=0; j<colintnum;j++) {A[i][j]=-(int) rand ()%201+ -; cout<<A[i][j]<<'\ t'; } cout<<Endl; }}intMaxarray (intArray[],intMaxsize,int&Location ) {    intmax=array[0];//Generate maximum Valuelocation=0;//Maximum numeric subscript position initialization for(intI=0; i<maxsize;i++)    {        if(max<Array[i]) {Max=Array[i]; Location=i; }    }    returnMax;}voidDivanum (inta[][ -],intRowintnum,intColintnum,intSubrownum,intSubcolnum,intEvetsum[],int&location,intTimes ) {    intsum[10000];//record the and of the sub-matricesintarraynum=0;//number of sub-matrices generatedintCount=0;//number of sub-matrices accumulated  for(introw=0; row<rowintnum-subrownum+1; row++)    {         for(intCol=0; col<colintnum;col++)//Find all sub-matrices{Sum[arraynum]=0;  for(intp=row;p<row+subrownum;p++)//the and of the sub-matrices is obtained            {                 for(intq=col;q<col+subcolnum;q++) {Sum[arraynum]+=a[p][q%Colintnum]; }} arraynum++;//record the number of sub-matrices generated}} Evetsum[times]=maxarray (sum,arraynum,location);//record the maximum number of sub-matrices in each generated sub-matrix}voidPrint (intRowintColintarray[][ -]){     for(intI=0; i<row;i++)    {         for(intj=0; j<col;j++)        {             out<<array[i][j]<<','; }         out<<'\ n'; }}voidMain () { out. Open ("Text.txt");    Srand ((unsigned) time (NULL)); intlocation[ -];//record the position of the maximum sub-array each timeinta[ -][ -],rowintnum,colintnum; intSubrownum,subcolnum; intmax=0, loctimes=0; intevetsum[10000]; intq=0;  while(q==0) {cout<<"Please enter the number of lines:"; CIN>>Rowintnum;  out<<RowIntNum<<','; cout<<"Please enter the number of columns:"; CIN>>Colintnum;  out<<ColIntNum<<','<<'\ n'; cout<<"integer Content"<<Endl; Randin (rowintnum,colintnum,a);//randomly generates an array of rowintnum rows, colintnum columnsPrint (rowintnum,colintnum,a);  for(subrownum=1; subrownum<=rowintnum; subrownum++)        {             for(subcolnum=1; subcolnum<=colintnum; subcolnum++) {divanum (a,rowintnum,colintnum,subrownum,subcolnum,evetsum,location[(Subrownum-1) *rowintnum+subcolnum-1], (subrownum-1) *rowintnum+subcolnum-1); }} cout<<"outputs the maximum matrix and"<<Endl; Max=maxarray (Evetsum, (subrownum-1) * (subcolnum-1), loctimes); cout<<max<<Endl;  out<<Max;  out. Close (); cout<<"whether to continue testing (enter 0 to continue)"<<Endl; CIN>>Q; System ("CLS"); }}

Program

Experimental summary

The harvest of this test is familiar with the percent and for loop statement of the effect of the array problem, for the time to achieve the complexity O (n), most of the methods are unable to meet the requirements, can only find new ideas for information, and then posted below.

Returns the maximum number of sub-arrays in an array of integers and 4

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.