Algorithm one day one--the maximum value of its sub-array for the input array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.