Given a sequence of integers S = {S1, S2, ..., Sn}, you shoulddetermine what is the value of the maximum positive product involving consecutive terms of S. Ifyou cannot find a positive sequence, you should consider 0 as the value of the maximum product.

Each test case starts with 1 ≤ N ≤ 18, the number of elements in a sequence. Each elementSi is an integer such that -10 ≤ Si ≤ 10. Next line will haveN integers, representing the value of each element in the sequence. There is a blank line aftereach test case. The input is terminated by end of file (EOF).

For each test case you must print the message: Case #M: The maximum product is P., where M isthe 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.

3
2 4 -3 5
2 5 -1 2 -1
Case #1: The maximum product is 8.

Case #2: The maximum product is 20.

枚举每一个长度组合,不断更新最大值,不过要注意有可能18个数都是10,那么最大值是多少?int还存得下吗?
#include"iostream"
#include"cstring"
using namespace std;
int main()
{
long long ans=-;
int ch,f;
long long an=;
int a[];
f=;
int n;
while(cin>>n)
{
for(int i=;i<n;i++)
{
cin>>ch;
a[i]=ch;
an*=ch;
if(an>ans)
{
ans=an;
}
}
for(int j=n-;j>=;j--)
{
an=;
for(int k=j;k>=;k--)
{
an*=a[k];
if(an>ans)
{
ans=an;
}
}
}
if(ans<) ans=;
cout<<"Case #"<<f++<<": The maximum product is "<<ans<<'.'<<endl;
cout<<endl;
ans=-;
an=;
} return ;
}
												

Maximun product的更多相关文章

  1. 最大乘积 Maximun Product

    最大乘积 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/B 题意: 输入n个元素组成的序列s,你需要 ...

  2. leecode 每日解题思路 152 Maximun Product Subarray

    问题描述: 问题链接:152 Maximum Product Subarray 在经典的算法解析中, 有关的分治和动态规划的,经典题型之一就是求最大子段和, 这道题就是他的变形:求最大子段积; 这个问 ...

  3. 枚举专项练习_Uva725(Division)_Uva11059(Maximun Product)

    //Uva725 #include <iostream> #include <cstring> #include <cstdlib> #include <cs ...

  4. uva 11059 maximum product(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK

  5. [LeetCode] Product of Array Except Self 除本身之外的数组之积

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  6. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. vector - vector product

    the inner product Givens two vectors \(x,y\in \mathbb{R}^n\), the quantity \(x^\top y\), sometimes c ...

  8. 1 Maximum Product Subarray_Leetcode

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  9. Leetcode Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. bind: Invalid argument

    出现此问题在于,listen函数在socket函数和bind函数之间. 例: /*客户端程序开始建立sockfd描述符*/ listenfd = socket(AF_INET,SOCK_STREAM, ...

  2. Codeforces Round #479 (Div. 3)解题代码

    A. Wrong Subtraction #include <bits/stdc++.h> using namespace std; int main() { int n,k; cin&g ...

  3. DFS Codeforces Round #306 (Div. 2) B. Preparing Olympiad

    题目传送门 /* DFS: 排序后一个一个出发往后找,找到>r为止,比赛写了return : */ #include <cstdio> #include <iostream&g ...

  4. CalService

    package org.crazyit.cal; import java.math.BigDecimal; /** * 计算业务类 * * @author yangenxiong yangenxion ...

  5. 自定义View(12)绘制.9图片

    代码如下: // 绘制.9图片 void draw9Path(Canvas canvas){ //创建一个ninePatch的对象实例,第一个参数是bitmap.第二个参数是byte[],这里其实要求 ...

  6. static属性

    static 属于全局,也就是类的属性 和方法,换句话说 一个类,不管有多少个实例,却只有一个全局变量 用static修饰的属性和方法称为静态属性和方法 需要注意的是 静态属性和方法属于类方法,加载类 ...

  7. 转-解决Mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost'问题

    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)   Red Hat Enterpr ...

  8. CoreText的绘制流程-转

    来自:http://blog.sina.com.cn/s/blog_7c8dc2d50101lbb1.html 使用coreText进行文本绘制,需要在工程中添加CoreText.framework, ...

  9. Hibernate配置(外部配置文件方式)

    配置Hibernate有2种方式,本文讲的是通过外部配置文件配置的方式 Hibernate核心配置文件 <?xml version='1.0' encoding='UTF-8'?> < ...

  10. js学习笔记-事件委托

    通过事件委托,你可以把事件处理器绑定到父元素上,避免了把事件处理器添加到多个子级元素上.从而优化性能. 事件代理用到了事件冒泡和目标元素.而任何一个元素的目标元素都是一开始的那个元素. 这里首先要注意 ...