您的位置:首页 > 其它

UVA11059 - Maximum Product

2016-04-14 17:50 323 查看
1、题目名称

Maximum Product

[b]2、题目地址[/b]

[b]https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2000[/b]

[b][b]3、题目内容[/b][/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.

大致意思就是,给出一个序列,问这个序列中最大连续累乘的子序列中,最大的值为多少,如果都为负数,则输出0.

感受: 一定要记得用long long , 还有格式问题,否则可能pe

#include"iostream"
using namespace std;
int a[20];
int main(){
int n,out;
out=0;
while(cin>>n){
for(int i=0;i<n;i++)
cin>>a[i];
long long max=0;
for(int i=0;i<n;i++)
for(int j=i;j<n;j++){
long long z=1;
for(int q=i;q<=j;q++)
z=z*a[q];
if(max<z)    max=z;
}
cout<<"Case #"<<++out<<": The maximum product is "<<max<<"."<<endl<<endl;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: