1、题目名称
Maximum Product
2、题目地址
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2000
3、题目内容
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.
大致意思就是,给出一个序列,问这个序列中最大连续累乘的子序列中,最大的值为多少,如果都为负数,则输出0.
感受: 一定要记得用long long , 还有格式问题,否则可能pe
1 #include"iostream" 2 using namespace std; 3 int a[20]; 4 int main(){ 5 int n,out; 6 out=0; 7 while(cin>>n){ 8 for(int i=0;i<n;i++) 9 cin>>a[i]; 10 long long max=0; 11 for(int i=0;i<n;i++) 12 for(int j=i;j<n;j++){ 13 long long z=1; 14 for(int q=i;q<=j;q++) 15 z=z*a[q]; 16 if(max<z) max=z; 17 } 18 cout<<"Case #"<<++out<<": The maximum product is "<<max<<"."<<endl<<endl; 19 20 } 21 }