Beauty of programming-maximum value of the sum of child Arrays

Source: Internet
Author: User

This classic example is available in the beauty of programming and the whole chapter of Chapter 8. Here we will not elaborate on the details of the algorithm, (I certainly haven't explained them clearly in these two books ......), However, I have solved the extension problem of the beauty of programming (the question of Pearl River is more profound and has not been answered in depth ).

 

The second question of expansion is described here:

This is based on the last algorithm. Each time when a [I]> nstart + A [I], that is, when nstart is greater than zero, the index of the starting array is updated, and the starting index of the final output should be reduced by one (you can think about why ~~); When Nall> nstart, update the ending index.

The result is displayed after the loop ends. This code has a bug that has not been debugged yet. When all input values are negative, the output result is incorrect ......

 

For the first question, my thinking is different from that in the book. According to the solution of the second question, we can easily obtain the initial index, and then make a correction to the loop, loop the array twice (it will stop when it is less than twice .), Make a record and jump out of the loop when the record value is reached. This is complicated to say, but the implementation of the code is relatively simple.

 

All the code is as follows:

 

// Beauty of programming-maximum sub-array and <br/> # include <iostream> <br/> using namespace STD; <br/> // function declarations <br/> int findmax1 (int * data, int BG, int end ); // n ^ 2 solution <br/> int findmax2 (int * data, int BG, int end ); // another N ^ 2 solution <br/> int findmax3 (int * data, int BG, int end ); // divide and conquer solution <br/> int findmax4 (int * data, int BG, int end); // dynamic programing solution <br/> int findmax5 (int * d ATA, int BG, int end ); // circus array <br/> // can find the index form begin to end of the array <br/> int findmax6 (int * data, int BG, int end, int & indexbg, Int & indexend); <br/> int main () <br/> {<br/> // function pointers <br/> int (* functions [5]) (int *, Int, INT) = {& findmax1, & findmax2, & findmax3, & findmax4, & findmax5}; <br/> int data [] = {1,-1, 2,-3,-5, 6, 7,-8 }; <br/> int data1 [] = {-9,-2,-1,-3,-4,-5,-6 ,- 7,-8 };< br/> for (INT I = 0; I <5; ++ I) <br/> {// use four functions <br/> cout <functions [I] (data,) <Endl; <br/>}< br/> int X, Y; <br/> // cout <findmax6 (data, 0, 9, x, y) <"/t" <x <ends <Y <Endl; <br/> // if this is the reason for Stack pressure, the output X and Y must be incorrect !!! <Br/> cout <findmax6 (data, 0, 9, x, y ); <br/> cout <"/t" <x <ends <Y <Endl; <br/> cout <"int max =" <int_max <Endl; <br/> return 0; <br/>}< br/> int inline maxtreeelement (int A, int B, int c) <br/>{< br/> return (A> B? A: B)> C? (A> B? A: B): C; <br/>}< br/> int findmax1 (int * data, int BG, int end) <br/> {// n ^ 2 solution <br/> int maxsum = data [0]; <br/> for (INT I = BG; I <end; ++ I) <br/>{< br/> int sum = 0; <br/> for (Int J = I; j <end; ++ J) <br/> {<br/> sum + = data [J]; <br/> maxsum = maxsum> sum? Maxsum: sum; <br/>}< br/> return maxsum; <br/>}< br/> int findmax2 (int * data, int BG, int end) <br/> {// another N ^ 2 solution <br/> // initialize <br/> int * realarray = new int [end-BG]; <br/> int * cumarr = realarray + 1; <br/> cumarr [-1] = data [0]; <br/> int maxsum = data [0]; <br/> for (INT I = BG; I <end; ++ I) <br/> cumarr [I] = cumarr [I-1] + data [I]; <br/> for (INT I = 0; I <end; ++ I) <br/>{< br/> int sum; <Br/> for (Int J = I; j <end; ++ J) <br/>{< br/> sum = cumarr [J]-cumarr [I-1]; <br/> maxsum = maxsum> sum? Maxsum: sum; <br/>}< br/> Delete [] realarray; <br/> return maxsum; <br/>}< br/> int findmax3 (int * data, int BG, int end) <br/> {// divide and conquer solution </P> <p> -- end; // make the index right </P> <p> If (bg> end) <br/> return-INT_MAX-1; // make sure that it cant change the right result <br/> If (BG = END) <br/> return data [BG]; <br/> int mid = bg + (end-BG)/2; <br/> int Lmax = data [BG], sum = 0; <br/> For (INT I = mid; I> = BG; -- I) <br/> {<br/> sum + = data [I]; <br/> Lmax = Lmax> sum? Lmax: sum; <br/>}< br/> int rmax = data [end]; <br/> sum = 0; <br/> for (INT I = end; i> mid; -- I) <br/>{< br/> sum + = data [I]; <br/> rmax = rmax> sum? Rmax: sum; <br/>}< br/> return maxtreeelement (Lmax + rmax, <br/> findmax3 (data, 0, Mid + 1 ), <br/> findmax3 (data, Mid + 1, end) <br/>); <br/>}< br/> int findmax4 (int * data, int BG, int end) <br/> {// dynamic programing solution <br/> int maxsum = data [0]; <br/> int maxindex = 0; <br/> for (INT I = BG; I <end; ++ I) <br/>{< br/> maxindex = (maxindex + data [I])> data [I]? (Maxindex + data [I]): Data [I]; <br/> maxsum = maxsum> maxindex? Maxsum: maxindex; <br/>}< br/> return maxsum; <br/>}< br/> int findmax5 (int * data, int BG, int end) <br/> {// Cirque array <br/> int maxsum = data [0]; <br/> int maxindex = 0; <br/> int indexbg = 0; <br/> for (INT I = BG; I <2 * End-1; ++ I) <br/>{< br/> Int J = I % end; <br/> if (I> = indexBg-1 + end) <br/> break; <br/> If (maxindex> 0) <br/> maxindex + = data [J]; <br/> else <br/> {<br/> maxindex = data [J]; <br/> indexbg = J; <BR/>}< br/> maxsum = maxsum> maxindex? Maxsum: maxindex; <br/>}< br/> return maxsum; <br/>}< br/> int findmax6 (int * data, int BG, int end, int & indexbg, Int & indexend) <br/>{< br/> int maxsum = data [0]; <br/> int maxindex = 0; <br/> for (INT I = BG; I <end; ++ I) <br/>{< br/> If (maxindex> 0) <br/> maxindex + = data [I]; <br/> else <br/> {<br/> maxindex = data [I]; <br/> indexbg = I; <br/>}< br/> If (maxsum <maxindex) <br/>{< br/> indexend = I; <br/> maxsum = maxindex; <br/>}< br/> return maxsum; <br/>}

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.