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)