LeetCode Maximum Product Subarray

Description

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个元素组成的序列S,你需要找出一个乘积最大的连续子序列。如果这个最大的乘积不是正数,应输出0(表示无解)。每个案例要用空格隔开。

题解:

连续子序列有两个要素:起点和终点,因此只需枚举起点和终点即可。

只要给最大值赋初值0,大于0才给最大值重新赋值,则任何负数最后输出都会为0,不需分开考虑。

注意:最大值要用long long类。

#include<iostream>
#include<cstdio>
using namespace std;
int a[];
int main()
{
int k=,t;
while(scanf("%d",&t)==)
{
long long maxn=;
for(int i=; i<t; i++)
cin>>a[i];
for(int i=; i<t; i++)
{
long long q=;
for(int j=i; j<t; j++)
{
q=q*a[j];
if(q>maxn)
maxn=q;
}
}
k++;
printf("Case #%d: The maximum product is %lld.\n",k,maxn);
printf("\n"); // 注意格式啊
}
return ;
}

LeetCode Maximum Product Subarray(枚举)的更多相关文章

  1. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

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

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

  3. Leetcode Maximum Product Subarray

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

  4. 152.[LeetCode] Maximum Product Subarray

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  5. [LeetCode] Maximum Product Subarray 连续数列最大积

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

  6. [leetcode]Maximum Product Subarray @ Python

    原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/ 解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘 ...

  7. LeetCode Maximum Product Subarray 解题报告

    LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...

  8. LeetCode Maximum Product Subarray 最大子序列积

    题意:给一个size大于0的序列,求最大的连续子序列之积.(有正数,负数,0) 思路:正确分析这三种数.0把不同的可能为答案的子序列给隔开了,所以其实可以以0为分隔线将他们拆成多个序列来进行求积,这样 ...

  9. DP Leetcode - Maximum Product Subarray

    近期一直忙着写paper,非常久没做题,一下子把题目搞复杂了..思路理清楚了非常easy,每次仅仅需更新2个值:当前子序列最大乘积和当前子序列的最小乘积.最大乘积被更新有三种可能:当前A[i]> ...

随机推荐

  1. 《Linear Algebra and Its Applications》-chaper3-行列式-从一个逆矩阵算法证明引入的行列式

    这一章节开始介绍线性代数中另外一个基本概念——行列式. 其实与矩阵类似,行列式也是作为简化表述多项式的一种工具,关于行列式的历史渊源,有如下的介绍. 在介绍逆矩阵的时候,我们曾提及二阶矩阵有一个基于矩 ...

  2. servelt乱码问题(tomcat服务端编码为ISO-8859-1)

    Post的编码决定机制: <meta http-equiv="Content-Type" content="text/html; charset=gb2312&qu ...

  3. linux 搭建vpn (pptp)

    一.VPN服务器环境说明 操作系统:CentOS release 6.4 (Final) 本地网卡: 复制代码 代码如下: # ifconfig em1 Link encap:Ethernet HWa ...

  4. C#中反射接受的字符串需要满足的Backus-Naur Form语法

    MSDN的Specifying Fully Qualified Type Names指明了C#中反射接受的字符串需要满足如下的Backus-Naur Form语法. BNF grammar of fu ...

  5. motan源码分析十:流量切换

    motan提供了流量切换的功能,可以实现把一个group的流量切换到另一个group(一个或多个服务都可以).大家可以使用tomcat部署motan的管理工具,并设置几个组,例如可以参考demo代码: ...

  6. 解决"the currently displayed page contains invalid values"

    原因是你的工程的根目录少了default.properties(有点项目工程这个文件名称是project.properties)这个文件,导致不能选择target:   解决办法: 在工程根目录下建立 ...

  7. Java程序员也应该知道的系统知识系列之(网卡,cpu,内存,硬盘,虚拟化)

    https://yq.aliyun.com/articles/1718?spm=5176.100240.searchblog.16.UaGd04 https://yq.aliyun.com/artic ...

  8. C++继承:公有,私有,保护

    前言 无论是在平时学习中还是还做项目之时,主要用到的继承都是 public 公有继承,因此,对protected private两者继承都不大了解! 今天,在看<Effective C++ 3e ...

  9. webpack入门学习

    一.认识webpack webpack是一款模块打包加载工具.它能够将各种资源.JS.CSS.图片等作为模块打包加载.本人也是近期因工作需要才开始接触webpack,在下文中有误的地方还希望各位能够指 ...

  10. php-fpm 启动参数及重要配置详解<转>

    原文地址  http://levi.cg.am/archives/3127 约定几个目录 /usr/local/php/sbin/php-fpm /usr/local/php/etc/php-fpm. ...