Pair member: Hyayan
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
Use the previous two-dimensional array to find the maximum subarray of the code and change the control conditions. In each loop, the first line loops through the number of len1, and Len1 is the number of columns in the array until the end of the loop, followed by the first column ...
Three. Source code
//A two-dimensional array loop. Cpp:defines The entry point for the console application.//#include"stdafx.h"#include"fstream.h"#include"iostream.h"#defineMAXSIZE 50//* * * * Read array information * * *voidReadarr (intArr[][maxsize],int&len1,int&len2) {Ifstream infile ("Arr.txt"); if(!infile) cout<<"Read failed! "<<Endl; Else{infile>>len1>>Len2; for(intI=0; i<len1;i++) { for(intj=0; j<len2;j++) {infile>>arr[i][j];//reading data from a filearr[i][j+len2]=Arr[i][j]; } } }}//* * * * Display MATRIX * * *voidShowarr (intArr[][maxsize],intLen1,intLen2,intSize1,intsize2) { for(inti=len1;i<=size1;i++)//to output an array from a file { for(intj=len2;j<=size2;j++) {cout<<arr[i][j]<<"\ t"; } cout<<Endl; }}//* * * * Summation formula * * *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++) {sum+=Arr[i][j]; } } returnsum;} intMainintargcChar*argv[]) { 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); cout<<"Matrix:"<<Endl; Showarr (arr,0,0, len1-1, len2-1); cout<<Endl; Line1=0; Line2=0; Row1=0; Row2=0; Sum=0; Max=arr[0][0]; for(intI=0; i<len1;i++)//number of rows in the first number { for(intj=0; j<len2;j++)//number of columns in the first number { for(intm=i;m<len1;m++)//number of rows in the second number { for(intN=j; (n<2*LEN2) && (n<j+3); n++) { //number of columns for the second numberSum=getsum (Arr,i,j,m,n);//Find out the matrix of these two numbers and if(sum>max) {Max=sum; Line1=i;//Save the first number of rowsLine2=m;//save a row for the second numberRow1=j;//Save the first number of columnsRow2=n;//Save column for second number} }}} cout<<"and the largest sub-matrix:"<<Endl; Showarr (ARR,LINE1,ROW1,LINE2,ROW2); cout<<"the largest and:"<<max<<Endl; return 0;}
Four. Results and
Five. Experience
Six. Pair photo
The and of the maximal subarray of the two-dimensional loop array