Maximum continuous and--DP

Source: Internet
Author: User

Enter a set of integers to find out the set of number sub-sequences and the maximum values. That is, only the maximum number of sub-sequences is required, and the largest sequence is not to be obtained. For example:

Sequence:-2 11-4 13-5-2, the maximum subsequence and 20.

Sequence:-6 2 4-7 5 3 2-1 6-9 10-2, the maximum subsequence and 16

#include <stdio.h>

int main ()
{
void res (int num[],int n);
int n;
while (scanf ("%d", &n)!=eof)
{
int num[110],i;
for (i=0;i<n;i++)
scanf ("%d", &num[i]);
Res (NUM,N);
}
return 0;
}
void res (int num[],int N)
{
int i,sum=0,max=0;
for (i=0;i<n;i++)
{
Sum=sum+num[i];
if (Sum>max)//continuous and larger than Max, assign to Max
Max=sum;
if (sum<0)//If consecutive numbers and becomes negative, then the next number larger than Max will be added from this number
sum=0;
}
printf ("%d\n", Max);
}

Maximum continuous and--DP

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.