UV 11059, uva11059
Given a sequence of integers S = {S1, S2,..., Sn}, you shoshould determine what is the value of
Maximum positive product involving consecutive terms of S. If you cannot find a positive sequence,
You shoshould consider 0 as the value of the maximum product.
Input
Each test case starts with 1 ≤ N ≤ 18, the number of elements in a sequence. Each element Si
Is
An integer such that −10 ≤ Si ≤ 10. Next line will have 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
File (EOF ).
Output
For each test case you must print the message: 'case # M: The maximum product is P. ', where
M is the number of the test case, starting from 1, and P is the value of the maximum product. After
Each test case 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.
Question: input the sequence S composed of n elements. You need to find a continuous subsequence with the maximum product. If the maximum product is not a positive number, 0 is output (indicating no solution ). 1 <= n <= 18-10 <= S <= 10
The output format is empty for each set of Output Cases (note)
Question Analysis: continuous subsequences have two elements: starting point and focus. Therefore, you only need to enumerate the start and end points. Since the maximum value of each element cannot exceed 10 and no more than 18 elements, the maximum product cannot exceed the 18 power of 10. So you can use long to store the data. (If int is used, the output result of 18 to 10 is incorrect. int cannot be stored)
The Code is as follows: (mom, I first thought of a three-cycle loop, and a silly loop of len represents the length of the sub-sequence. Even though I have tried many cases, I just can't go through it. I thought about it for a moment. I wrote it with a try, and then the TM passed, which is also RLGL .....)
1 #include <stdio.h> 2 int a[20]; 3 int main() 4 { 5 int n,N=0,c2=0; 6 while(scanf("%d",&n)==1) 7 { 8 long long c,c2=0; 9 ++N;10 for(int i=0; i<n; i++)11 scanf("%d",&a[i]);12 for(int q=0;q<n;q++)13 {14 c=1;15 for(int z=q;z<n;z++)16 {17 c*=a[z];18 if(c>c2)19 c2=c;20 }21 }22 if(c2<=0)23 printf("Case #%d: The maximum product is 0.\n\n",N);24 else25 printf("Case #%d: The maximum product is %lld.\n\n",N,c2);26 }27 return 0;28 }