Maximum sub-array 2

Source: Internet
Author: User

Title: 1. Enter an array of integers with positive numbers in the array and negative values. 2. One or more integers in the array make up a sub-array, each of which has a and. 3. If the array a[0] ... A[j-1] next to each other, allowing a[i-1], ... A[n-1],a[0] ... A[J-1] and the largest. 4. Returns the position of the maximum subarray at the same time. 5. Ask for the maximum value of all sub-arrays. Workaround: On the previous basis, an array of rings was added, and the subscript value of the array was required to be returned. Implementation method: Calculate the value of the largest subarray of the ring array starting with each number, then calculate the maximum value of those values, record the subscript value of the array that produces the maximum value, and then output the subscript value. Team : Wang Yu http://home.cnblogs.com/u/wangjianly/ knot group Photo:The code is as follows:
1#include <iostream>2 using namespacestd;3 4 voidMain ()5 {6     inta[ -] = {0 };7     intb[Ten][ -] = {0 };8     intLength, I, j, all =0;9     intBegin =0, end =0;//starting and terminating subscripts for arraysTen     intMax, temp; Onecout <<"Enter a random integer:"<<Endl; A      -      for(length =0;;) -     { theCIN >>A[length]; -length++; -         if(GetChar () = ='\ n')//end of carriage return -         { +              Break; -         } +     } A                                   //To find a sub-array at      for(i =0; i<length; i++) -     { -temp =i; -all =0; -j =0; -          while(J <= Length-1) in         { -All + =A[temp]; toB[I][J] =All ; +temp++; -             if(Temp>length-1) the             { *temp =0; $             }Panax NotoginsengJ + +; -         } the     } +max = b[0][0]; A      for(i =0; i<length; i++)//The maximal subarray of each number corresponds to the maximum subarray of the whole integer group. the     { +          for(j =0; j<length; J + +) -         { $             if(b[i][j]>max) $             { -Max =B[i][j]; -Begin =i; theEnd =J; -             }Wuyi         } the     } -cout <<"the maximum number of sub-arrays is:"<< Max <<Endl; Wucout <<"The index position of the element in the maximum subarray:"<<Endl; -i =0; About      while(I <= end)//output Subscript Value $     { -cout << begin <<"  "; -begin++; -         if(Begin >=length) A         { +Begin =0; the         } -i++; $     } thecout <<Endl; the}

The results of the operation are as follows:

After testing, the maximum subarray in the array should be "3" "4" "5" "6" "0" The summation result is: 11

The results are correct!

After testing, the maximum subarray in the array should be "0" "1" "2" "3" "4" The summation result is: 11

The results are correct!

The development of this pair of projects is summarized as follows:

<

 4.5 TD class= "Et3" width= "height=" >
Project Plan Summary:
Date \ Task Lectures Writing Programs Access information Total Day
Thursday 2 0.5 0 2.5  
Saturday &NBSP;  2 2.5
 
Sunday &NBSP; 1
1.5  2.5  
Weekly totals 2 3.5 4 9.5

<

Time log:
Date Start time End time Interrupt Time Quiet time Activities Note
March 24 14:00 15:50 10min 100min Lectures + programming Software engineering
 3 month 26th 8:20 11:50  60min 2.5h program + lookup material Check out the relevant ideas/programming
15:00 17:30 30min 2h programming + information Debug program looking for error place
March 27 8:00 11:00 30min 2.5h Summarize and optimize the code Reflection and optimization

<

Bug log:
Date Number Introduction phase Exclusion phase Repair Time & Problem description
March 24 1 Coding Compile 10min, array problem
March 26 2 Coding Compile

50min, program implementation ideas have problems

March 27 3 Coding Compile

50min;

Code implementation is too cumbersome--optimized code

Maximum sub-array 2

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.