Returns the and of the largest sub-arrays in an integer array, and refines the analysis:
1, in all sub-arrays ending in element tail, select the element and the largest sub-array, TAIL=1,2...N.
2, and the largest sub-array ending with element K is the TAIL-1 that contains the end of the element and the largest subarray, or only the element tail this element, there are altogether these two optional States.
3, after getting the maximum sub-array at the end of each element, as long as the maximum is the maximum of all the sub-arrays
4, the type of the array element is defined as _int32, and the array element is randomly generated.
The code is as follows:
//Euna Lee Wang Shuo 2016.3.25#include <iostream>#include<algorithm>#include<time.h>#defineRAND16 (rand () <<1) + (rand () &1))# define WIDE4294967296using namespacestd;#defineMAX 1000Long Long intA[max];Long Long intTail[max]; //dynamic programming ideas, Time complexity O (n)intMain () {intLength//Array Length intI//Loop Variable intTail//record where the array endsSrand ((_int32) time (NULL)); cout<<"Please input the length of array:"<<Endl; CIN>>length; cout<<"randomly generated."<<length<<"number is"<<Endl; for(i=1; i<=length; i++) {A[i]= (RAND16 << -) + RAND16;//generating 32-bit array elementscout<<a[i]<<Endl; } //calculates the maximum value of the sum of the sub-arrays ending in tail, that is, the sub-array contains the number of Ktail[1] = a[1]; for(tail=2; tail<=length; tail++)//Tail a stage{Tail[tail]= Max (a[tail],tail[tail-1]+A[tail]); } //only two states//because and the largest subarray must end with a certain number, the maximum value of this length sub-array is taken Long Long intall = tail[1]; for(i=2; i<=length; i++) All=Max (all, tail[i]); cout<<"MAX:"<<All<<" !"<<Endl;}
Result
A one-dimensional array that evaluates the maximum number of sub-arrays (first adjacent 32 bits)