题目:
Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the
maximum positive product involving consecutive terms of S. If you cannot find a positive sequence,you should consider 0 as the value of the maximum product.InputEach test case starts with 1 ≤ N ≤ 18, the number of elements in a sequence. Each element Siisan integer such that −10 ≤ Si ≤ 10. Next line will have N integers, representing the value of eachelement in the sequence. There is a blank line after each test case. The input is terminated by end offile (EOF).OutputFor each test case you must print the message: ‘Case #M: The maximum product is P.’, whereM is the number of the test case, starting from 1, and P is the value of the maximum product. Aftereach test case you must print a blank line.Sample Input32 4 -352 5 -1 2 -1Sample OutputCase #1: The maximum product is 8. Case #2: The maximum product is 20.题目大意:输入个长度为n的序列,要你找到乘积最大的连续序列的积。(如果乘积小于0,就相当于乘积为0)
题目思路:既然要找连续的序列,给一个循环枚举起点,一个循环枚举终点,一个循环它起点到终点的元素乘起来,给个MAX变量赋值为0(因为乘积小于0点都会被赋值为0)。
X每个乘积都与MAX比较,比MAX大的,就把它赋值给MAX。循环完毕,输出MAX的值就行了。(还要注意输出案例时要连着输出2个换行)
代码:
1 #include2 #include 3 #include 4 using namespace std; 5 const int maxn=18+5; 6 int main() 7 { 8 int n,i,a[maxn],j,k,p=0; 9 long int sum,max; 10 while(cin>>n&&n)11 { 12 for(i=0;i >a[i];14 max=0;15 for(i=0;i max)27 max=sum;28 }29 }30 printf("Case #%d: The maximum product is %lld.\n\n",++p,max);31 32 }33 return 0;34 }