The algorithm is the soul of programming, the essence of programming thought ———— algorithm one day
/********************************************************************created:2015 January 19 00:20:59 author:jackery T (n) =0 (n) Purpose: This procedure is an array of input, the maximum value of its sub-array; *********************************************************************/# Include "stdafx.h" #include <iostream>typedef int datatype;datatype arrlen (DataType array[]); void subaarr_maxsum (DataType array[], DataType len); using namespace Std;void main () {DataType array[]={0,12,-11,5};D atatype length=sizeof ( Array)/sizeof (DataType); Subaarr_maxsum (array,length);} void Subaarr_maxsum (DataType array[], DataType len) {if (NULL = = Array | | Len <=0) {return;} int cursum = 0, subaarr_maxsum = 0;//First consider that the array has positive, negative, or 0 cases for (int i=0; i<len; i++) {cursum + = array[i];//Cumulative if (Cursum < 0) {//current and less than 0, reset to 0curSum = 0;} Current and greater than maximum and, then reset Max and if (Cursum > subaarr_maxsum) {subaarr_maxsum = Cursum;}} Next consider the case where the elements in the array are all zeros if (subaarr_maxsum = = 0) {subaarr_maxsum = array[0];for (int i=1; i<len; i++) {if (Array[i] > Subaarr_maxsum) {subaarr_maxsum = Array[i];}}}cout << "Maximum sub-array of the and:" << Endl; cout << "subaarr_maxsum=" << subaarr_maxsum<< Endl;}
To be improved:
1. Implement the dynamic input array;
2. How to find the elements of the array containing the largest array in the Subarray and output;
Algorithm one day one--the maximum value of its sub-array for the input array