One-dimensional array of contiguous sub-arrays of the largest and

Source: Internet
Author: User

Title Description

Enter an array of integers with positive and negative numbers in the array. One or more consecutive integers in an array make up a sub-array. The maximum value for the and of all sub-arrays.

Enter a description

Input has multiple sets of data, each set of test data consists of two rows. The first behavior is an integer n (0<=n<=100000), and when n=0, the input ends.

The next line contains n integers (we guarantee that all integers belong to [ -1000,1000]).

Output description

For each test case, you need to output a single line of integers representing the maximum and the continuous sub-vectors.

Input sample

3-1 -3 -2

Output sample

-1

Test code

1#include <stdio.h>2 3 intMain ()4 {5     intN, I, cur, ans;6     inta[Ten];7scanf"%d", &n);8      for(i =0; I < n; i++)9     {Tenscanf"%d", &a[i]); One     } AAns = cur = a[0]; -      for(inti =1; I < n; i++) -     { thecur = cur + a[i] > a[i]? Cur +A[i]: a[i]; -ans = ans > cur?ans:cur; -     } -printf"%d\n", ans); +     return 0; -}

One-dimensional array of contiguous sub-arrays of the largest and

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.