The Problem description is given to an integer sequence (which may have positive numbers, 0 and negative numbers), and its maximum continuous subsequence product is obtained. For example, if an array a = {3,-4,-5, 6,-2} is given, the maximum continuous subsequence product is 720, that is, 3 * (-4) * (-5) * 6 = 720. The product of the maximum continuous subsequence is different from that of the maximum continuous subsequence, because positive, negative, or zero. Assume that the array is a [] and the dynamic return is used directly to solve the problem. Considering the possible negative number, we use Max [I] to represent the product value of the maximum continuous subsequence ending with a [I, min [I] indicates the product value of the smallest continuous subsequence ending with a [I]. The state transition equation is: Max [I] = max {a [I], max [I-1] * a [I], Min [I-1] * a [I]}; Min [I] = min {a [I], max [I-1] * a [I], Min [I-1] * a [I]}; initial state is Max [0] = Min [0] = a [0]. The Code is as follows:
# Include "iostream" using namespace std; int max3 (int a, int B, int c) {int t = a> B? A: B; return t> c? T: c;} int min3 (int a, int B, int c) {int t = a <B? A: B; return t <c? T: c;} int max_multiple (int * a, int n) {int * Min = new int [n] (); int * Max = new int [n] (); min [0] = Max [0] = a [0]; int max = Max [0]; for (int I = 1; I <n; I ++) {Max [I] = max3 (Max [I-1] * a [I], Min [I-1] * a [I], a [I]); // calculate the maximum value Min [I] = min3 (Max [I-1] * a [I], Min [I-1] * a [I], a [I]); // calculate the minimum value of the three values, if (max <Max [I]) max = Max [I];} // memory release delete [] Max; delete [] Min; return max;} // implementation method without saving intermediate variables int max_multiple_2 (int * a, int n) {int minsofar, maxsofar, max; max = minsofar = maxsofar = a [0]; for (int I = 1; I <n; I ++) {int maxhere = max3 (maxsofar * a [I], minsofar * a [I], a [I]); int minhere = min3 (maxsofar * a [I], minsofar * a [I], a [I]); maxsofar = maxhere; minsofar = minhere; if (max <maxsofar) max = maxsofar;} return max ;}int main () {int a [] = {3,-4, 0, 6,-2}; cout <max_multiple_2 (a, 5) <endl; system ("pause"); return 0 ;}