Pair development My teammate Wang Chunfan, study number 20132902 blog address http://www.cnblogs.com/-beauTiFul/
Design ideas:
Input array 1-4-2 7
A[0]=sz[0];1
A[1]=max (A[0]+sz[1],sz[1]);-3
A[2]=max (a[1]+sz[2],sz[2]);-2
A[3]=max (A[2]+sz[3],sz[3]); 7
Max (A[0],a[1]...a[n])
Also use the idea of the array 1, because it can be connected to the end, it is converted to the {1,-4,-2,7}{-4,-2,7,1}{-2,7,1,-4}{7,1,-4,-2} The maximum number of arrays, and then the sum of the sub-arrays.
#include <iostream>#include<algorithm>#include<cstdio>#include<vector>using namespacestd;intMain () {inti,j, SZGS,SZGS1, maxsum; intsz[ -], a[ -];//Maximum Arraycout <<"Please enter the number of array elements:"; CIN>>Szgs; cout<<"Please enter an array element:"; for(i =0; i < Szgs; i++) {cin>>Sz[i]; } a[0] = Maxsum = sz[0]; SZGS1=Szgs; if(szgs>0) { for(j =0; J < Szgs; J + +) { for(i = j +1; i < SZGS1; i++) {A[i]= Max (A[i-1] +Sz[i], sz[i]); Maxsum=Max (Maxsum, A[i]); } SZ[SZGS1]= sz[szgs1-Szgs]; A[j+1] = sz[szgs1-szgs+1]; SZGS1=szgs1+1; }} cout<<"the number of the largest sub-arrays is:"<< Maxsum <<Endl;}
Array 2--array end-to-end, to find the largest sub-array