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]> ...
随机推荐
- [Locked] Inorder Successor in BST
Inorder Successor in BST Given a binary search tree and a node in it, find the in-order successor of ...
- Codeforces Round #324 (Div. 2)解题报告
---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...
- myeclipse 闪退解决方法
今天提交一个svn文件发生卡死,然后关闭myeclipse后发生,打开myeclipse出险闪退,摸索半天,发现: 只要修改下工作空间的名称,然后打开myeclipse重新导入即可,只是之前的配置都没 ...
- Hibernate学习笔记(一):级联删除
一对多的关系映射 在一的一方配置文件中将会配置set节点信息: *.hbm.xml配置文件中set节点的属性: Lazy:默认是true 即使用延迟加载,false表示即时加载: Order-by:一 ...
- ok6410 u-boot-2012.04.01移植六完善MLC NAND支持
继ok6410 u-boot-2012.04.01移植四.五后,开发板基本已支持MLC NAND,支持DM9000.但是通过NAND命令更新u-boot到NAND,还存在问题,需要根据u-boot的n ...
- php session_set_save_handler 函数的用法(mysql)(转)
<?php /*============================文件说明======================================== @filename: s ...
- 模仿ios下的coverflow
Android高级图片滚动控件,编写3D版的图片轮播器 http://blog.csdn.net/guolin_blog/article/details/17482089 A cool Open So ...
- BestCoder冠军赛 - 1005 Game 【DP】
[题意] 给出一个set,set中有几个数. 现在给出n个人,环成一圈搞约瑟夫... 开始时从第1号报数,每次从set中随机选出一个数s,等报数到s后,报s的人出圈,其他人继续报数. 最后只剩1人时, ...
- python文件处理--笔记
之前一段时间一直在忙着写毕业论文,盲评搞得人心惶惶,以致于都没有看书,最近需要补上前面落下的东西. <Python3程序开发指南> 1.二进制数据的读写 二进制格式通常是占据磁盘空间最小. ...
- MySQL中TIMESTAMP和DATETIME区别
1.两者的存储方式不一样 TIMESTAMP:把客户端插入的时间从当前时区转化为UTC(世界标准时间)进行存储.查询时,将其又转化为客户端当前时区进行返回. DATETIME:不做任何改变,基本上是原 ...