Problem D - Maximum Product

Time Limit: 1 second

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.

思路:

确定七点和终点,进行枚举,然后推断符不符合要求。

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
int S[20];
int main()
{
int n,casex=1;
long long ans,a;
while(scanf("%d",&n)!=EOF)
{
ans=0;
for(int i=1;i<=n;i++)
scanf("%d",&S[i]);
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
a=1;
for( int k=i;k<=j;k++)
{
a*=S[k];
}
ans=max(ans,a); }
}
printf("Case #%d: The maximum product is %lld.\n\n",casex,ans);
casex++;
}
return 0;
}

uva11059(最大乘积)的更多相关文章

  1. 7_2 最大乘积(UVa11059)<枚举连续子序列>

    给一个数字集合{ S1,S2,…,Sn },请从这个数字集合里找出一段连续数字,使他们的乘积是最大的.以Case 1为例子,2 x 4 = 8为这个集合的最大乘积:而Case 2则为2 x 5 x(– ...

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

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

  3. C语言 · 最小乘积(基本型)

    问题描述 给两组数,各n个. 请调整每组数的排列顺序,使得两组数据相同下标元素对应相乘,然后相加的和最小.要求程序输出这个最小值. 例如两组数分别为:1 3 -5和-2 4 1 那么对应乘积取和的最小 ...

  4. 最大连续子序列乘积(DP)

    题目来源:小米手机2013年校园招聘笔试题 题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 输入: 输入可能包含多个测试样例.每个测试样例的第一行仅包含正整数 ...

  5. 给定一个double类型的数组arr,其中的元素可正可负可0,返回子数组累乘的最大乘积。例如arr=[-2.5,4,0,3,0.5,8,-1],子数组[3,0.5,8]累乘可以获得最大的乘积12,所以返回12。

    分析,是一个dp的题目, 设f[i]表示以i为结尾的最大值,g[i]表示以i结尾的最小值,那么 f[i+1] = max{f[i]*arr[i+1], g[i]*arr[i+1],arr[i+1]} ...

  6. CentOS 7 ssh脚本 求两个数值的“和”,“乘积”,”商“

    1:在目录下创建一个  sh文件 touch 1.sh 2:进入sh文件书写sh脚本 #!/bin/bash read -p "请输入第一个数值" A read -p " ...

  7. NOIP2000乘积最大[序列DP]

    题目描述 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友XZ也有幸得 ...

  8. n数乘积第m小

    这是从Java贴吧看到的一道面试题,看了别人的解题思路实现的.... 如题: n个数,他们的乘积可得到一些其它的数,求第m小的. 输入格式: n m n1 n2 n3 ... 例: 输入: 3 8 2 ...

  9. 【wikioi】1017 乘积最大

    题目链接 算法:划分型DP 非常典型的一道题目,划分型DP 题目描述: 设有一个长度为N的数字串,要求选手使用K个乘号将它分成K+1个部分,找出一种分法,使得这K+1个部分的乘积能够为最大.同时,为了 ...

随机推荐

  1. .NEL IL实现对象深拷贝

    对于深拷贝,通常的方法是将对象进行序列化,然后再反序化成为另一个对象.例如在stackoverflow上有这样的解决办法:https://stackoverflow.com/questions/785 ...

  2. Java中的Redis应用

    1.配置redis集群   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?xml version="1.0" encoding ...

  3. 利用C#来做ASP.NET的登陆页面

    一.新建一个数据库 新建一个access数据user.mdb. 新建一个user表,添加:UserId(文本类型)及Password(文本类型)两个字段.二.新建一个default.aspx文件. 在 ...

  4. Android 安全加密

    Android 安全加密 引言     对称加密.非对称加密.消息摘要.数字签名等知识都是为了理解数字证书工作原理而作为一个预备知识.数字证书是密码学里的终极武器,是人类几千年历史总结的智慧的结晶,只 ...

  5. 【转】MYSQL DBA知识了解-面试准备

    http://www.itpub.net/forum.php?mod=viewthread&tid=1825849 公司招聘MySQL DBA,也面试了10个2年MySQL DBA工作经验的朋 ...

  6. hibernate:There is a cycle in the hierarchy! 造成死循环解决办法

    下面是报的异常:在网上搜了关于:There is a cycle in the hierarchy!,才知道原来是因为死循环造成的!解决了好久,没有成功,后台不得已请教老大,老大说是因为在使用JSON ...

  7. Oracle数据库应用

    Oracle数据库应用 一:.Oracle数据库应用知识 二:表空间和用户权限管理 表空间: 表空间是数据逻辑结构的一个重要组件,表空间可以存放各种应用对象,如表,索引.而每个表空间由一个或者多个数据 ...

  8. Python 面向对象(五) 描述器

    使用到了__get__,__set__,__delete__中的任何一种方法的类就是描述器 描述器的定义 一个类实现了__get__,__set__,__delete__中任意一个,这个类就是描述器. ...

  9. Jenkins与网站代码上线解决方案

    1.1 前言 Jenkins是一个用Java编写的开源的持续集成工具.在与Oracle发生争执后,项目从Hudson项目独立. Jenkins提供了软件开发的持续集成服务.它运行在Servlet容器中 ...

  10. pytesseract使用

    1.安装pip install pytesseract 2.安装tesseract-ocr,下载地址:https://github.com/UB-Mannheim/tesseract/wiki,我安装 ...