How to find the maximum value of the sum of several consecutive numbers _c language

Source: Internet
Author: User
Given a set of numbers, there are positive and negative, the sum of several consecutive numbers of the maximum value? and find out from the beginning of the first few, the number of the end? If more than one sequence can make up the same maximum value, select the first sequence. (Note: The beauty of programming is seen in these two days. Found in section 2.14, the maximum value of the sum of the array's sub arrays is very similar to this one. But there is no request to start drinking the end of the position, only to find the maximum value, the solution is similar to the following code, but only two variables, no array, do time complexity O (n), Space complexity O (1))
Implemented with program design. I realized a way to share with you, if friends you have a better way to solve this problem, I hope you can reply, and share with you.
Other than that if the title is changed into : "Given a set of numbers, there are positive and negative, the sum of several consecutive numbers of the maximum value?" and find out from the beginning of the first few, the number of the end? If you have more than one sequence that can make up the same maximum, select the one with the fewest digits in the sequence. "How to write the program, I think for a long time did not think of a good way. Ask for directions!!!!!!!
here is the C + + code I implemented:
Copy Code code as follows:

#include "stdafx.h"
#include <iostream>
using namespace Std;
int getmaxs (int arrary[],const int n)
{
int max = 0, temp = 0; Max Max, temp temporary variable
int begin = 0, end = 0;
int *maxlist= new Int[n]; Maxlist[n], an array that is generally larger than the original array
max = maxlist[0] = arrary[0];
Begin = end = Arrary[0];
for (int i = 1; i < n; i++)
{
temp = Maxlist[i-1] + arrary[i];
if (temp > Arrary[i])
{
Maxlist[i] = Maxlist[i-1] + arrary[i]; Updating the maximum array of values
}
Else
{
Maxlist[i] = Arrary[i]; Updating the maximum array of values
}
if (Maxlist[i] > Max)
{
max = Maxlist[i]; Update maximum value with current value
end = i; Set Endpoint Index
}
}
The next few lines are looking for the starting point
Int j = end, sum = 0;
while (J < n && sum!= max)
{
Sum + + arrary[j];
J--;
}
begin = ++j;
cout << "The max sum is:" << Max << Endl;
cout << "The Begin position is:" << ++begin << Endl;
cout << "The end position are:" << ++end << Endl;
return 0;
}
int _tmain (int argc, _tchar* argv[])
{
int a[] = { -1,-1,-1,1,1,1,-1,-1,-1,-1,1,1,-1,1,1,-1};
int a[] = {5,8,12,-25,-15,56,-14,25,2,-10,6};
int n = sizeof (a)/sizeof (a[0]);
GETMAXS (A,n);
return 0;
}

Another very similar topic: there is an array of a[n], there are only two kinds of numbers:-1 and 1. I and J are two integers, assuming that 0 <= i < j <= N-1, find the sum of the number of consecutive numbers in A[i] to A[j (if the largest part is equal, the shortest one is preferred).
This problem and the above problem similar, but I still can't think how to take the shortest, only to get the maximum value for the first time.

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.