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[30];
int main()
{
    int k=0,t;
    while(scanf("%d",&t)==1)
    {
        long long maxn=0;
        for(int i=0; i<t; i++)
            cin>>a[i];
        for(int i=0; i<t; i++)
        {
            long long q=1;
            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 0;
}
时间: 2024-12-26 21:20:17

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

LeetCode: Maximum Product Subarray &amp;&amp; Maximum Subarray

Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 对于Product

[leetcode] Maximum Product Subarray @ python

[leetcode] Latest added: 2014-09-23 Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. Trick:

LeetCode——Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 原题链接:https://oj.leetcode.com/problems/max

[LeetCode] Maximum Product Subarray的两种思路

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 这题和之前的一题Maximum Subarray非常类似,一个是求最大和,而这个是

[LeetCode]Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 这道题是找出数组中的一个子序列,要求这个子序列中数的乘积是所有子序列中最大的. 如

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

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. Hide Tags Array Dynamic Programming 这个问题是给

LeetCode Maximum Product Subarray 解题报告

LeetCode 新题又更新了,最大子数组乘积 https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子数组的最大乘积. 解题思路:最简单的思路就是3重循环,求解productArray[i][j]的值(productArray[i][j]为A[i]到A[j]的乘积),然后记录其中最大值,算法时间复杂度O(n3),必然TLE. 第一次优化,动态规划,求解:productArray[i][j]的时候不用再次循

DP Leetcode - Maximum Product Subarray

最近一直忙着写paper,很久没做题,一下子把题目搞复杂了..思路理清楚了非常简单,每次只需更新2个值:当前子序列最大乘积和当前子序列的最小乘积.最大乘积被更新有三种可能:当前A[i]>0,乘以前面最大的数(>0),得到新的最大乘积:当前A[i]<0,乘以前面最小的数(<0),得到新的最大乘积:A[i]它自己>0,(A[i-1]==0.最小乘积同理.. class Solution { public: int Max(int a, int b, int c) { int ma

【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product =