Team Development One (the maximum value of the sum of successive sub-arrays of an array)

Source: Internet
Author: User
Tags random seed

Team Development One (the maximum value of the sum of successive sub-arrays of an array)

(1) Design idea: In general, the sum of the largest sub-arrays of an array is the sum of the first few numbers and the next number in order of the array, set a variable to hold the larger number after each comparison, and then proceed to the array terminal; But given the continuous subarray, it should be thought that in addition to sequential It is also necessary to consider the end and the first end of the continuous, so in order to solve the array sequence is not necessarily the sum of the largest sub-arrays, so we must also solve the largest sub-array in this case, the method is at the same time from the two ends of the array to find the sum of their largest sub-arrays, Then, after summing up before the meeting and comparing the sum of the largest sub-arrays previously asked, take the largest of them as the sum of successive maximal sub-arrays. (Because the sum of the largest sub-arrays obtained from one end alone is greater than the sum of the maximum number of words found on both sides of the same side, it guarantees the non-redundancy of the program.) )

(2) The problem arises:

A: The user enters an array length that conflicts with the computer's memory space allocation: for example, the array length is not allowed to be 0, the complex number is greater than the memory space that is reconciled with the computer, or the range of the array is greater than the space;

B: The upper and lower bounds of the user input do not meet the logic requirements, for example, the lower limit is greater than the upper limit, or the upper limit exceeds the integer number range;

(3) Possible solution: for the above two cases, only need to be in the user interface with the system to interact with the corresponding test function, the user then follow the system's prompt step-by-step;

(4) Source code:

//The sum of the contiguous maximum subarray of an array//Li min, Mar 22th#include <iostream>#include<time.h>using namespacestd;intMain () {intN, I, J, k,a[ the],b,c; Charmm;  Do{cout<<"Please enter the length of the array:"<<Endl; CIN>>N; //checking the legitimacy of input array lengthscout<<"Please enter two number as the limit of the array range, where the first number is the lower limit, the first number is the upper limit:"<<Endl; CIN>>b>>C; if(n==0|| n<0|| N> the|| n>c-b) {cout<<"the input array length is not valid, please re-enter! "<<Endl; CIN>>N; }    //detect the legitimacy of the input array range    if(b>c| | (b<-2147483648|| b==-2147483648)|| c==2147483647) {cout<<"the number entered is not valid, please re-enter:"<<Endl; CIN>>b>>C; } srand ((unsigned) time (NULL)); //the Srand () function produces a random seed that starts at the current time     for(i=0; i<n; i++) {A[i]=b+rand ()% (c-b+1); } cout<<"the random number generated by the system is:"<<Endl;  for(i=0; i<n; i++) {cout<<a[i]<<'\ t'; } //The maximum and the current sub-array are calculated in order of the array    intt = a[0]; intsum =BT;  for(k=1; k<n; k++) {T= Max (a[k],t+A[k]); Sum=max (sum, t); }  //Considering that the array is contiguous (that is, the array can be visualized as a circle), the sum of the maximal sub-arrays of the two ends that are not met before each other are simultaneously made from the first and end of the array .    ints = a[0];  for(i=1; I<n && s+a[i]>s; i++) s+=A[i]; T=A[n];  for(j=n-1; j>=1&& t+a[j]>t; j--) T+=A[j]; //compares the sum of the two largest sub-arrays that were not met before the two ends of the array, and the sum of the largest sub-arrays that were calculated in order of the array in turn    if(I<j && s+t>sum) sum= S+t;//the sum of the maximum number of words in two casescout<<"The sum of the maximum number of word groups is:"<<sum<<Endl; cout<<Endl; cout<<"*************************************************************"<<Endl; cout<<"continue Please enter y exit Please enter n"<<Endl; cout<<"*************************************************************"<<Endl; CIN>>mm;}  while(mm=='y'|| mm=='Y');} 

(4) Experiment:

(6) Summary: Team development is actually a good development model, we can pair from each other to learn from each other to benefit from the things, as well as enhance their sense of cooperation, for a difficult problem, a person is always isolated, but if the integration of two people's solutions, Even if the difficult problem can divide and conquer, so we should actively invest in the project development team.

Cooperation team: Li Min Liu Zixiang

Team Development One (the maximum value of the sum of successive sub-arrays of 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.