Solving the maximal sub-problems of sequences by divide-and-conquer algorithm

Source: Internet
Author: User

In particular, when all integers in a sequence are negative integers, their maximum is 0.

1#include <stdio.h>2 3 intCalumaxsubsum (int*array,intLeftintRight );4 5 intMain ()6 {   7     intarray[6] = {2, -2,3,1, -4,2};8     intLen_array =sizeof(array)/sizeof(array[0]);9     inti =0;Ten     intSubsum = calumaxsubsum (Array,0, Len_array); One      for(i =0; i < Len_array; i++) A     { -printf"%d", Array[i]); -     } the      -printf"\ n"); -     //printf ("Max Son and yes:"); -printf"subsum =%d\n", subsum);  +     return 0; - } +  A intCalumaxsubsum (int*array,intLeftintRight ) at {    -    intsum =0; -    if(Left >=Right ) -    { -       if(Array[left] >0) -       { insum =Array[left]; -       } to       Else +       {    -sum =0;  the       } *    } $    ElsePanax Notoginseng    { -       intCenter = (left + right)/2; the       intS1 =0, S2 =0; +       intLefts =0, rights =0; A       inti =0; the       intLeftsum =calumaxsubsum (array, left, center); +       intRightsum = calumaxsubsum (array, center +1, right); -        for(i = center; I >= left; i--) $       { $Lefts + =Array[i]; -          if(Lefts >S1) -          { theS1 =lefts; -          }Wuyi       } the        -        for(i = center +1; I < right; i++) Wu       { -Rights + =Array[i]; About           if(Rights >S2) $           { -S2 =Rights; -           }           -       } A        +sum = S1 +S2; the       if(Sum <leftsum) -       { $sum =leftsum; the       } the       if(Sum <rightsum) the       { thesum =rightsum; -       } in    } the     the    returnsum; About}

Where is the key idea?

Solving the maximal sub-problems of sequences by divide-and-conquer algorithm

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.