HUAS Summer Trainning #3 B

题目:

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.
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 of
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.

题目大意:输入个长度为n的序列,要你找到乘积最大的连续序列的积。(如果乘积小于0,就相当于乘积为0)

题目思路:既然要找连续的序列,给一个循环枚举起点,一个循环枚举终点,一个循环它起点到终点的元素乘起来,给个MAX变量赋值为0(因为乘积小于0点都会被赋值为0)。

X每个乘积都与MAX比较,比MAX大的,就把它赋值给MAX。循环完毕,输出MAX的值就行了。(还要注意输出案例时要连着输出2个换行)

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 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<n;i++)
13             cin>>a[i];
14         max=0;
15         for(i=0;i<n;i++)
16         {
17             for(j=i;j<n;j++)
18             {
19                 sum=1;
20                 for(k=i;k<=j;k++)
21                 {
22                     sum*=a[k];
23                 }
24                 if(sum<0)
25                     sum=0;
26                 if(sum>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 }
时间: 2024-11-25 18:21:15

HUAS Summer Trainning #3 B的相关文章

HUAS Summer Trainning #3~E

Description Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are calledequivalent in one of the two cases: They are equal. If we split string a into two halves of the same siz

2015 HUAS Summer Trainning #6~H

Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识.  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数.  给定一个区间,你能计算出这个区间内有多少个美素数吗? Input 第一行输入一个正整数T,表示总共有T组数据(T <= 10000). 接下来共T行,每行输入两个整数L,R(1<= L <= R <= 10

2015 HUAS Summer Trainning #4 B

Description Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so calledscribers. The scriber had been given a book and after several months he finished its copy. One of

2015 HUAS Summer Trainning #4 C

My birthday is coming up and traditionally I’mserving pie. Not just one pie, no, I have a numberN of them, of various tastes and of various sizes. Fof my friends are coming to my party and each ofthem gets a piece of pie. This should be one pieceof o

2015 HUAS Summer Trainning #4 D

Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the minimalamount of them, such they would completely cover the segment [0, M]. InputThe first line is the number of test cases, followed by a blank line.Eac

2015 HUAS Summer Trainning #5~E

Description The world financial crisis is quite a subject. Some people are more relaxed while others are quite anxious. John is one of them. He is very concerned about the evolution of the stock exchange. He follows stock prices every day looking for

2015 HUAS Summer Trainning #4 A

Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input seque

HUAS Summer Trainning #3~L

Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It

HUAS Summer Trainning #3~M

Description 我们知道,在编程中,我们时常需要考虑到时间复杂度,特别是对于循环的部分.例如, 如果代码中出现 for(i=1;i<=n;i++) OP ; 那么做了n次OP运算,如果代码中出现 fori=1;i<=n; i++)   for(j=i+1;j<=n; j++) OP; 那么做了n*(n-1)/2 次OP 操作. 现在给你已知有m层for循环操作,且每次for中变量的起始值是上一个变量的起始值+1(第一个变量的起始值是1),终止值都是一个输入的n,问最后OP有总共多

2015 HUAS Summer Trainning #5~N

Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … The bone collector had a big bag with a volume of V ,and