01-1. Maximum child columns and issues (20)

Source: Internet
Author: User

01-1. Maximum child columns and issues (20)

Given a sequence of K integers {N1, N2, ..., NK}, "Continuous child column" is defined as {Ni, ni+1, ..., Nj}, where 1 <= i <= J <= K. "Maximum child columns and" are defined as the and the largest of all contiguous child column elements. For example, given sequence {-2, 11,-4, 13,-5,-2}, its contiguous sub-columns {11,-4, 13} have the largest and 20. You are now asked to write a program that calculates the maximum sub-columns of a given integer sequence.

Input format:

Enter line 1th to give the positive integer k (<= 100000), and the 2nd line to give the k integers, separated by a space.

Output format:

Outputs the maximum child columns in a row. If all integers in the sequence are negative, the output is 0.

Input Sample:
6-2 11-4 13-5-2
Sample output:
20

#include <stdio.h>#include<stdlib.h>intMaxsubseqsum (Const intData[],intdatalen); intMain () {intNsize=0, I; int* dataarray,*Pdataitem; scanf ("%d",&nSize); DataArray=(int*) malloc (nsize*sizeof(int)); Pdataitem=DataArray;  for(i=0; i<nsize;i++) {scanf ("%d", pdataitem++); } printf ("%d\n", Maxsubseqsum (dataarray,nsize));        Free (DataArray); return 0; }              intMaxsubseqsum (Const intData[],intdatalen) {        intcurrentsum,maxsum,j; Currentsum=maxsum=0;  for(j=0; j<datalen;j++) {currentsum+=Data[j]; if(currentsum>maxsum) Maxsum=currentsum; Else if(currentsum<0) Currentsum=0; }        returnmaxsum; }
View Code

01-1. Maximum child columns and issues (20)

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.