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]> ...
随机推荐
- 5th day
感觉mysql里面的东西好多,很容易忘记,不是很熟练,知道某些函数有某种功能,但就是想不起来函数的名字,需要去翻笔记,真的还需要大量的练习,毕竟这块的内容可以说是全新的...不知道后面做项目怎么样,这 ...
- java关键字synchronized
1.对于synchronized方法和synchronized块,一个线程访问时,其他线程可以访问此类的非synchronized方法或块,不能访问其他带synchronized的方法或块. 举例如下 ...
- Android Bitmap开发之旅--基本操作
1 Bitmap加载方式 在介绍Bitmap--OOM 异常时,首先介绍一下Bitmap有哪几种加载方式.通常Bitmap的加载方式有Resource资源加载.本地(SDcard)加载.网络加载等加载 ...
- 在 foreach 里使用引用要注意的陷阱(转)
从一道面试题开始 在开始本节内容前,我们先来看看一道还算比较常见的PHP面试题: 1 $arr = array('1','2','3'); 2 3 foreach($arr as &$v) ...
- Linux: FTP服务原理及vsfptd的安装、配置
1.FTP 服务的安装# yum install -y vsftpd [root@rusky pub]# ls -l /etc/vsftpd/ total 20 -rw-------. 1 root ...
- 【开源java游戏框架libgdx专题】-08-中文显示与绘制
libgdx虽然是由美国人Mario Zechner(即BadlogicGames)写的开源引擎,由于Libgdx底层是用OpenGL实现的,所以Libgdx是可以支持中文的,在libgdx中的汉字都 ...
- 使用MiniProfiler调试Asp.net Mvc性能
使用nuget添加MiniProfiler.EF组件 在Global文件中配置MiniProfiler protected void Application_Start() { AreaRegistr ...
- 在ASP.NET MVC5 及 Visual Studio 2013 中为Identity账户系统配置数据库链接及Code-First数据库迁移
在ASP.NET MVC5 及 Visual Studio 2013 中为Identity账户系统配置数据库链接及Code-First数据库迁移 最近发布的ASP.NET MVC 5 及Visual ...
- 织梦DeDeCms列表分页和内容页分页错位解决办法
文章页分页代码在这里/include/arc.archives.class.php列表页分页/include/arc.listview.class.php 很多入门的站长会碰到这样的问题,织梦的通病, ...
- firebug中console命令尝试
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...