PTA Data structure and Algorithm topic set (Chinese) 5-1 max sub columns and problems (20 points)

Source: Internet
Author: User

Given the sequence of KK integers {n_1n 1, n_2n 2, ..., n_kn K}, continuous sub columns are defined as {n_in I, n_{i+1}n i+1, ..., N_jn J}, where 1 \le i \le j \ Le k1≤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.

This theme tests the performance of various algorithms in various data situations. The characteristics of each set of test data are as follows: Data 1: With the sample equivalent, test basic correctness; Data 2:102 random integers; Data 3:103 random integers; data 4:104 random integers; data 5:105 random integers; Input format:

Enter line 1th to give a positive integer KK (\le 100000≤100000), and the 2nd line gives a KK integer, which is separated by a space. 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

Program code:

#include <iostream>
using namespace std;

int main ()
{
	int N;
	int num[100001]={0};
	int max=0,sum=0;
	cin>>n;
	for (int i=0;i<n;i++)
		cin>>num[i];
	for (int i=0;i<n;i++)
	{for
		(int j=i;j<n;j++)
		{
			sum=sum+num[j];
			if (Sum>max)
				max=sum;
		}
		sum=0;
	}
	cout<<max;
} Stupid Way



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.