PAT 01-1. Max Sub Columns and problems (20)

Source: Internet
Author: User
01-1. Max sub-columns and questionsTime limit 10000 ms
Memory Limit 65536 KB
Code length limit 8000 B
The Standard of the Judgment procedure

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

Input Format:

The input line 1th gives a positive integer k (<= 100000), and line 2nd gives K integers, which are separated by spaces.

output Format:

Outputs the largest 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
Output Sample:
20
/*
PROBLEM: Max Sub column and Problem
solving method: Online processing
time complexity: O (n)/

#include <stdio.h>
#include <malloc.h>

int findseq (int n,int list[]);//online processing

int main ()
{
	int length,i;
	int *list;
	int max=0;
	scanf ("%d", &length);
	List= (int*) calloc (length,sizeof (int));
	for (i=0;i<length;i++) {
		scanf ("%d", &list[i]);
	Max=findseq (length,list);
	printf ("%d", max);
	return 0;
}

int findseq (int n,int list[]) {
	int thissum,maxsum=0;
	int i;
	thissum=0;
	for (i=0;i<n;i++) {
		thissum+=list[i];	
		if (thissum>maxsum)
			maxsum=thissum;
		else if (thissum<0)
				thissum=0;
	}
	return maxsum;
}


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.