Title: Returns the maximum number of sub-arrays in an integer array and
requirement : To connect the end of the array to form a ring, and return the position of the largest sub-array
Design Ideas :
1, the ring is broken, become a line, analysis of the largest sub-array on this line, find the largest sub-array, you can find the largest sub-array start and end.
2, then the end point as a starting point, the starting point as the end point, the line into a ring, in the search for the largest sub-array (not counting the beginning and end of the search).
3, the last found in the two largest sub-arrays joined together to form the largest subarray of this integer ring, and then the record start and end point output, return the maximum sub-array position.
Source:
#include <iostream>#defineN 100using namespacestd;voidMain () {intA[n], b[n][n]; intLength, I, j, W =0, p =0, q =0, temp, m; cout<<"Enter a random integer"<<Endl; for(length =0;;) {cin>>A[length]; Length++; if(GetChar () = ='\ n') { Break; }} cout<<"The length of this array is:"<< length <<Endl; //To find a sub-array for(i =0; i<length; i++)//two cycles, the elimination method, to determine the maximum number of sub-arrays{m=i; W=0; J=0; while(J <= Length-1) {W+=A[m]; B[I][J]=W; M++; if(M>length-1) {m=0; } J++; }} Temp= b[0][0]; for(i =0; i<length; i++)//The maximal subarray of each number corresponds to the maximum subarray of the whole integer group. { for(j =0; j<length; J + +) { if(b[i][j]>temp) {Temp=B[i][j]; P=i; Q=J; } }} cout<<"the maximum number of sub-arrays is:"<< Temp <<Endl; cout<<"The subscript of the element in the maximum sub-array is seated:"<<Endl; I=0; while(I <=q) {cout<< P <<" "; P++; if(P >=length) {P=0; } I++; } cout<<Endl;}
Test Results :
Summary: This experiment is mainly to use the idea of ring insertion, need to fully consider the connection problem. So we use a large array to compare the two different starting points respectively.
Teammate: Lu Guanghao website: http://www.cnblogs.com/lvstudy/
Returns the maximum number of sub-arrays in an integer array (enhanced)