First, title and requirements:
Returns the maximum number of sub-arrays in an integer array and
If the array a[0] ... A[j-1] Next to the end, allow A[i-1] ... A[n-1],a[0] ... A[J-1] and returns the position of the largest sub-array at the same time.
Second, design ideas:
For this notebooks to take the one-dimensional array method, but because of the integration of the array, the position can not be judged. So this method is decidedly not implemented.
The little partner used to say that the method provided by the students in the class, form a fixed-length window, and then add the comparison. This is a good way. Unfortunately, time complexity is not n.
So I looked up some information on the Internet. Thought is a little quoted on the internet. First of all, there will be two cases, one spanning a[n-1],a[0]. One is not cross-over.
There are many ways to not leap. For a span of 0 points, it can be converted to the smallest of its sub-arrays. Because the sum of the arrays is determined. For the middle that paragraph must be minimal and. The sum is minimized and the maximum is calculated. For two Max and compare. Get the final result.
Third, the Code
/*Wang Dongbo Yanya*/#include<iostream.h>#include<stdlib.h>structret{intMax,start,end;//for storing the maximum value, and the beginning and ending position};structRET MAX2 (intArry[],intLength//across Arry[n-1], Arry[0], the largest and{ intTotal=0; intstart1=0; intStart2;//Start Position intEnd=0; intsum=arry[0]; intminsum=arry[0]; for(intI=1; i<length;i++) { if(sum>0) {sum=Arry[i]; Start1=i; } Else{sum=sum+Arry[i]; } if(minsum>=sum) {Minsum=sum; End=i; Start2=Start1; } Total=total+Arry[i]; } Total=total+arry[0]; Minsum=total-minsum; structRET ret1={minsum,start2,end}; returnRet1;}structRET MAX1 (intArry[],intLength//not spanning 0 points of maximum and{ intstart1=0; intStart2;//Start Position intEnd=0; intsum=arry[0]; intmaxsum=arry[0]; for(intI=1; i<length;i++)//find the smallest and most adjacent arrays { if(sum<0) {sum=Arry[i]; Start1=i; } Else{sum=sum+Arry[i]; } if(maxsum<=sum) {Start2=Start1; End=i; Maxsum=sum; } } structRET ret1={maxsum,start2,end}; returnRet1;}intMain () {srand (unsigned) time (0)); intN; cout<<"number of input elements:"; CIN>>N; inta[ -]; for(intI=0; i<n;i++) {A[i]=rand ()% --Ten; cout<<a[i]<<" "; } cout<<Endl; structRET w=max2 (a,n);//Call the MAX2 function to find the maximum value across 0 points structRET q=max1 (a,n); if(w.max>Q.max) {cout<<"Max and for:"<<w.max<<"Starting Position:"<<w.end+1<<"End Position:"<<w.start-1; } Else{cout<<"Max and for:"<<q.max<<"Starting Position:"<<q.start<<"End Position:"<<Q.end; } return 0;}
Four
V. Summary of the Experiment
For this experiment is still quite sentiment, for some things can not be too complicated. Like the last one-dimensional array can be easily resolved. But he fills the snake with his feet. A few more unwanted code. But also a realization of their own ideas.
The experiment was a bit borrowed from the Internet. From the opposite direction, it is a good way to remind me of a speech in Ted, all things have to consider the opposite, the Americans in Japan to ask the name of the street, the answer is such a block and a block. Because the streets are not named.
The idea of thinking has been elaborated in detail. Here it is only the reverse thinking of this thought.
Whatever the matter, it seems that both sides need to be considered.
Finally, we have to attach the fatigue of hard thinking:
The largest and oldest array of arrays