LeetCode Maximum Product Subarray(枚举)
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(枚举)的更多相关文章
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Leetcode Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 152.[LeetCode] Maximum Product Subarray
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] Maximum Product Subarray 连续数列最大积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode]Maximum Product Subarray @ Python
原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/ 解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘 ...
- LeetCode Maximum Product Subarray 解题报告
LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...
- LeetCode Maximum Product Subarray 最大子序列积
题意:给一个size大于0的序列,求最大的连续子序列之积.(有正数,负数,0) 思路:正确分析这三种数.0把不同的可能为答案的子序列给隔开了,所以其实可以以0为分隔线将他们拆成多个序列来进行求积,这样 ...
- DP Leetcode - Maximum Product Subarray
近期一直忙着写paper,非常久没做题,一下子把题目搞复杂了..思路理清楚了非常easy,每次仅仅需更新2个值:当前子序列最大乘积和当前子序列的最小乘积.最大乘积被更新有三种可能:当前A[i]> ...
随机推荐
- iOS开发ARC入门和使用
本文引自:http://www.onevcat.com/2012/06/arc-hand-by-hand/ 英文原版:http://www.raywenderlich.com/5677/beginni ...
- [Java] JavaMail 发送 html 格式、带附件的邮件
本案例演示发送 html 格式,可带附件的邮件发送.发送纯文本邮件的例子可参照上一篇博文JavaMail 简单案例. EmailHelper, Email 的帮助类,向帮助类提供 SMTP 服务器域名 ...
- String Problem - HDU 3374 (kmp+最大最小表示)
题目大意:有一个字符串长度为N的字符串,这个字符串可以扩展出N个字符串,并且按照顺序编号,比如串 ” SKYLONG “ SKYLONG 1 KYLONGS 2 YLONGSK 3 LONGSKY ...
- 用Myeclipse 编写struts.xml时,自动提示
之所以不自动提示,是因为这个xml它不知道自己的xml格式是什么有哪些标签,所以不知道该怎么提示 所以就要给它引入格式,所以要引入XSD或者DTD文件 1.首先打开MyEclipse的窗口,选择“Wi ...
- JavaScript交换两个变量值的七种解决方案
前言 这篇文章总结了七种办法来交换a和b的变量值 1 2 var a = 123; var b = 456; 交换变量值方案一 最最最简单的办法就是使用一个临时变量了,不过使用临时变量的方法实在是太l ...
- gradle 集成到myeclipse
新的项目用到gradle,所以学了下,地址:http://dist.springsource.com/release/TOOLS/gradle :help 下,安装好,重启即可,gradle作为mav ...
- CentOS 6.4的安装--史上最全-CRPER木木
安装工具及软件: VmwareWorkstation9 CentOS-6.4-x86_64-LiveCD Vmware初始设置: 刚装好的VMWARE启动后,虽说默认已经设置好基础 ...
- android圆角View实现及不同版本这间的兼容
在做我们自己的APP的时候,为了让APP看起来更加的好看,我们就需要将我们的自己的View做成圆角的,毕竟主流也是将很多东西做成圆角,和苹果的外观看起来差不多,看起来也还不错. 要将一个View做成圆 ...
- ScriptManager的使用方法
脚本管理控件(ScriptManger)是ASP.NET AJAX中很重要的控件,通过使用ScriptManger可以进行整个页面的局部更新的管理.ScriptManger用来处理页面上局部更新,同一 ...
- 查看Oracle数据库被锁住的表,删除锁表的进程
锁表处理及查询 查看Oracle数据库被锁住的表,删除锁表的进程 1.查看被锁住的表 SELECT dob.object_name table_name, lo.locked_mode, lo. ...