The development of software engineering pair The sum of successive maximal sub-arrays in an array

Source: Internet
Author: User

First, team members:

Tan Wei, Chen Zhili

Second, the project name:

Sum of successive maximal sub-arrays in an array

Third, our design ideas:

Set Sum[i] is the contiguous subarray with the end of the first element and the largest. For element I, all the lengths of the sub-arrays ending with the elements in front of it have been evaluated, then the contiguous subarray ending with the element I and their sum largest is either the i-1 element ending with the sum of the elements, the contiguous subarray with the largest of them, or only the element I, i.e. sum[i] = max ( SUM[I-1] + arr[i], arr[i]). The choice can be made by judging whether Sum[i-1] + arr[i] is greater than arr[i], which is actually equivalent to judging whether sum[i-1] is greater than 0. So I and Chen Zhili follow this idea programming as follows:

Four, the code:

#include <iostream.h>void main () {while (1) {int length;int *arr=new int[length];cout<< "Please enter this array length:";cin> >length;cout<< "Please enter the array:"; for (int i=0;i<length;i++) {cin>>arr[i];} int result = Arr[0];int sum = arr[0];for (i=1;i<length;i++) {if (sum > 0) {sum + = Arr[i];} Else{sum =arr[i];} if (sum >result) result = sum;} cout<< "The sum of the largest contiguous subarray in the array is:" <<result<<endl;cout<< "----------------------------------------" <<endl;}}

V. Operation and Testing:

Tested when all negative, full positive, all is 0, there is a negative and a variety of special cases, etc., are running correctly.

Vi. Summary:

Although this is not the first time to do the project, but the process of obvious feeling to solve the problem more thoughtful than a person to consider the overall, efficiency, improve a lot of mutual learning and cooperation more smoothly.

Seven, work photo

The development of software engineering pair The sum of successive maximal sub-arrays in an array

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.