Maximum Connectivity sub-array
After several iterations of the project to develop an array of items, the teacher then arranged the "Unicom array" task:
1. Title: Returns the number of the largest interconnected subarray in a two-dimensional integer group;
2, there are positive numbers in the array, there are negative numbers;
3, the maximum value of all sub-arrays, requiring a time complexity of O (n);
4, the program to use the array placed in the Input.txt file, the format of the file: the number of rows, the number of columns, each row of elements;
First, the experimental ideas
Second, the Experiment Code and implementation
The code is as follows:
1 //2016/4/1--Zhao Ziyin & Hole Aerospace2 3#include <iostream>4#include <fstream>5 using namespacestd;6 7 intMax (intNintArr[],int*start_mark,int*Final_mark)8 {9 intstep[ -] = {0};//Step Records Each step of the computed sub-array andTen intI, sum =0, Max1 =0; One /*Sum is a sub-array and A The MAX1 is the maximum and the sub-array - */ - for(i =0; i<n; i++) the { - if(sum<0) -sum =Arr[i]; - Else +sum = sum +Arr[i]; -Step[i] =sum; + } AMAX1 = step[0]; at for(i =0; i<n; i++) - { - if(max1<Step[i]) - { -Max1 =Step[i]; -*final_mark =i; in } - } to for(i = *final_mark; I >=0; i--) + { - if(Step[i] = =Arr[i]) the { **start_mark =i; $ Break;Panax Notoginseng } - } the returnmax1; + } A the voidMain () + { - intm, N, I, J, Start_mark, Final_mark, big; $ intMax1; $ intread[10000];//read the character set of a file - intup[ -], down[ -], h[ -]; - intarr2[ -][ -], arr1[ -]; the /*array of M row n columns - Start_mark indicates the starting coordinate of the largest subarrayWuyi Final_mark represents the terminating coordinate of the largest subarray the big represents the last output of the largest Unicom sub-array and - Max1 is the largest subarray of one-dimensional arrays returned by the function and Wu Up holds maximum sub-array start coordinates per line - Down holds maximum sub-array termination coordinates per line About h holds the maximum number of sub-arrays per row and $ ARR2 storing two-dimensional arrays - Arr1 A one-dimensional array of splits - */ - A /*cout << "Please enter the number of rows and columns of the two-dimensional array:"; + cin >> m >> N; the cout << "Please enter this two-dimensional matrix:" << Endl; - For (i = 0; i<m; i++) $ { the For (j = 0; j<n; j + +) the { the cin >> A[i][j]; the } - }*/ in the //file Input theIfstream infile ("Input.txt"Ios::inch); About if(Infile.is_open () = =false) the { theCerr <<"Open error!"<<Endl; theExit1); + } -InFile >> read[0];//read number of rows M them = read[0];BayiInFile >> read[1];//read number of columns n then = read[1]; thecout <<"in the specified file"<< read[0] <<"Line"<< read[1] <<"the two-dimensional array of columns is as follows:"<<Endl; - for(i =0; I < m; i++)//reading an array and outputting it in format - { the for(j =0; J < N; J + +) the { theinfile >> Read[i +2]; theARR2[I][J] = read[i +2]; -cout << Arr2[i][j] <<" "; the if(j% (N-1) ==0&& J! =0) the //if (j = = n-1) thecout <<Endl;94 } the } the infile.close (); the 98 //decompose a two-dimensional array into several one-dimensional arrays by rows About for(i =0; i<m; i++) - {101 for(j =0; j<n; J + +)102 {103ARR1[J] =Arr2[i][j];104 } theMax1 = Max (n, Arr1, &start_mark, &Final_mark);106Up[i] =Start_mark;107Down[i] =Final_mark;108H[i] =Max1;109 } the 111Big = h[0]; the for(i =0; i +1<m; i++)113 { the if(Up[i] <= down[i +1] && Down[i] >= up[i +1])//Unicom, then add theBig + + H[i +1]; the for(j = up[i]; J<up[i +1]; J + +)117 {118 if(Arr2[i +1][j]>0)//is independent positive, there is119Big + + Arr2[i +1][j]; - }121 }122 123cout <<"The number of the largest interconnected sub-arrays of this two-dimensional array is:"<<Endl;124cout << Big <<Endl; the 126}
The results of the operation are as follows:
Third, the experience of experimental experiences
Maximum Connectivity sub-array