Maximum sub-sequences and

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h>int main () {int this_sum, max_sum, Old_first, Old_last, New_first;int N  , I, TMP, flag = 1;int first = 1;int *data;scanf ("%d", &n); this_sum= max_sum = Old_first = New_first = 0;old_last = N- 1;data = (int *) malloc (n * sizeof (int));    for (i = 0; i < n; i + +)    {        scanf ("%d", &data[i]);    } for (i = 0; i < n; i++) {this_sum + = data[i];/* to the right to accumulate */if (first)//to find the largest and {if (this_sum >= 0) {= 0;old_first = Old_ last = i;}} if (flag)//Gets the beginning of the new This_sum {if (this_sum >= 0) {flag = 0;new_first = i;}} if (This_sum > max_sum) {max_sum = this_sum;/* found larger and, then update current results */old_last = i;if (New_first! = Old_first) {Old_first = New_f Irst;}} else if (This_sum < 0)/* If the current sub-sequence and is negative */{this_sum = 0;/* It is not possible to make the back and increase, discard the */flag = 1;}} printf ("%d%d%d\n", Max_sum, Data[old_first], data[old_last]); return 0;}

Maximum sub-sequences 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.