Two-dimensional arrays end to end, like one end-to-end tape, to find the maximum value of all sub-arrays and

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
#include <iostream> #include <time.h> #include <conio.h> #include <fstream>using namespace std ; ofstream out;void Randin (int rowintnum,int colintnum,int a[][100])//randomly generate a two-dimensional array {for (int. i=0;i<rowintnum;i++) {for ( int j=0;j<colintnum;j++) {a[i][j]=-(int) rand ()%201+100;cout<<a[i][j]<< ' \ t ';} Cout<<endl;}} int maxarray (int array[],int maxsize,int &location) {int max=array[0];//generate maximum value location=0;//maximum numeric subscript position initialize for (int i=0 ; i<maxsize;i++) {if (Max<array[i]) {max=array[i]; Location=i;}} return Max;} void Divanum (int a[][100],int rowintnum,int colintnum,int subrownum,int subcolnum,int evetsum[],int &Location,int times) {int sum[10000];//records the number of sub-matrices and int arraynum=0;//generates a sub-matrix of int count=0;//sub-matrix cumulative number for (int row=0;row< rowintnum-subrownum+1;row++) {for (int col=0;col<colintnum;col++)//Ask all sub-matrices {sum[arraynum]=0;for (int p=row;p< row+subrownum;p++)//Get sub-matrix and {for (int q=col;q<col+subcolnum;q++) {sum[arraynum]+=a[p][q%colintnum];}} arraynum++;//records the number of sub-matrices generated}}evetsum[times]=maxarRay (sum,arraynum,location);//Record the maximum number of sub-matrices in each generated sub-matrix}void Print (int row,int col, int array[][100]) {for (int i=0;i<row; i++) {for (int j=0;j<col;j++) {out<<array[i][j]<< ', ';} out<< ' \ n ';}} void Main () {Out.open ("Text.txt"), Srand ((unsigned) time (NULL)), int location[100];//record the position of each maximum sub-array int a[100][100], Rowintnum,colintnum;int subrownum,subcolnum;int max=0,loctimes=0;int evetsum[10000];int q=0;while (q==0) {cout< < "Please enter the number of lines:";cin>>rowintnum;out<<rowintnum<< ', ';cout<< "Please enter the number of columns:"; cin>>colintnum;o ut<<colintnum<< ', ' << ' \ n ';cout<< ' integer content ' <<endl; Randin (rowintnum,colintnum,a);//randomly generate rowintnum rows, colintnum array print (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<< "Output maximum Matrix and" <<endl;max=maxarray (Evetsum, (SUBROWNUM-1) * (SubColNum-1), loctimes); Cout<<max<<endl;out<<max;out.close ();cout<< "Do you want 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.

Two-dimensional arrays end to end, like one end-to-end tape, to find the maximum value of all sub-arrays and

Related Article

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.