leetcode Maximum Product subarray
Description
Given a sequence of integers S = {S1, S2, ..., Sn}, you should determine what is the value of the maximum positive prod UCT involving consecutive terms of S. If you cannot find a positive sequence, you should consider 0 as the value of the maximum product.
Input
Each test case is starts with 1≤n≤18, and the number of elements in a sequence. Each element Si was an integer such that−10≤si≤10. Next Line would has N integers, representing the value of each element in the sequence. There is a blank line after each test case. The input is terminated by end of file (EOF).
Output
Must print the message: ' Case #M: The maximum product was P. ', where M is the number of the the test CAs E, starting from 1, and P is the value of the maximum product. After each test, you must print a blank line.
Sample Input
3 2 4-3 5 2 5-1 2-1
Sample Output
Case #1: The maximum product is 8.
Case #2: The maximum product is 20.
Test instructions
To enter a sequence of n elements, you need to find a continuous sub-sequence with the largest product. If the maximum product is not a positive number, it should be output 0 (indicates no solution). Each case should be separated by a space.
Exercises
Successive sub-sequences have two elements: start and end, so you simply enumerate the start and end points.
As long as the maximum value is assigned to the initial 0, greater than 0 to the maximum value is re-assigned, then any negative number of the final output will be 0, do not need to consider separately.
Note: The maximum value should be in the long long class.
#include <iostream>#include<cstdio>using namespacestd;inta[ -];intMain () {intk=0, T; while(SCANF ("%d", &t) = =1) { Long Longmaxn=0; for(intI=0; i<t; i++) Cin>>A[i]; for(intI=0; i<t; i++) { Long Longq=1; for(intJ=i; j<t; J + +) {Q=q*A[j]; if(q>maxn) MAXN=Q; }} k++; printf ("Case #%d:the Maximum product is%lld.\n", K,MAXN); printf ("\ n");//watch the format. } return 0;}
Leetcode Maximum Product Subarray (enumeration)