A. Questions asked
Topic ### (1): and maximum continuous subarray (maximum and sub-segment) ### ### BACKGROUND problem: Given n integers (may be negative) sequence consisting of a [1], a [2 ] , a [3], ..., a [n], find the sequence as a [i] + a [i + 1] + ... + a [j] and the maximum value of the sub-segments. When the definition given sub-segments ### are negative integer and is 0, so defined, the required optimal values: Max {0, a [i ] + a [i + 1] + ... + a [ j]}, 1 <= i <= j <= n ### For example, when (a [1], a [ 2], a [3], a [4], a [5], a [6] ) = (- 2,11, -4,13, -5, -2), and the maximum of 20 sub-segment. ### - quote from the "Baidu Encyclopedia" two write code # Code # three programs operating results. # Four-Cell test ### test cases (-5,11, -4,13, -5, -2). path ### performs a (beginning) b (temp = a [i ]) d (null) f (temp> 0?) c (temp = temp + a [i]) e (sum = temp) #include "pch .h "#include" CppUnitTest.h "
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1){
int n = 6,i;
int a[6] = { -5,11,-4,13,-5,-2 };
int sum = 0, temp = 0;
for ( i = 0; i < n; i++) {
if (temp > 0) {
temp =temp+ a[i];
}
else temp = a[i];
if (temp > sum) {
sum = temp;
} }
int real = 20;
Assert::AreEqual(sum, real);
}
};
} # V. Test Results