Member: Yanya 20122914
Wang Dongbo 20122823
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.
A fixed-length window can be formed to add a comparison in turn.
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
#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
Through this experiment I found that our cooperation is more and more tacit understanding, our communication is also in a very pleasant atmosphere, has been noisy before. There are two cases of this experiment, one of which spans a[n-1],a[0] and one that has not been crossed. 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.
Six, group photo
Pair development array of the end-to-end arrays of the most Yamato