uva11059(最大乘积)
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(最大乘积)的更多相关文章
- 7_2 最大乘积(UVa11059)<枚举连续子序列>
给一个数字集合{ S1,S2,…,Sn },请从这个数字集合里找出一段连续数字,使他们的乘积是最大的.以Case 1为例子,2 x 4 = 8为这个集合的最大乘积:而Case 2则为2 x 5 x(– ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- C语言 · 最小乘积(基本型)
问题描述 给两组数,各n个. 请调整每组数的排列顺序,使得两组数据相同下标元素对应相乘,然后相加的和最小.要求程序输出这个最小值. 例如两组数分别为:1 3 -5和-2 4 1 那么对应乘积取和的最小 ...
- 最大连续子序列乘积(DP)
题目来源:小米手机2013年校园招聘笔试题 题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 输入: 输入可能包含多个测试样例.每个测试样例的第一行仅包含正整数 ...
- 给定一个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]} ...
- CentOS 7 ssh脚本 求两个数值的“和”,“乘积”,”商“
1:在目录下创建一个 sh文件 touch 1.sh 2:进入sh文件书写sh脚本 #!/bin/bash read -p "请输入第一个数值" A read -p " ...
- NOIP2000乘积最大[序列DP]
题目描述 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友XZ也有幸得 ...
- n数乘积第m小
这是从Java贴吧看到的一道面试题,看了别人的解题思路实现的.... 如题: n个数,他们的乘积可得到一些其它的数,求第m小的. 输入格式: n m n1 n2 n3 ... 例: 输入: 3 8 2 ...
- 【wikioi】1017 乘积最大
题目链接 算法:划分型DP 非常典型的一道题目,划分型DP 题目描述: 设有一个长度为N的数字串,要求选手使用K个乘号将它分成K+1个部分,找出一种分法,使得这K+1个部分的乘积能够为最大.同时,为了 ...
随机推荐
- ⑩bootstrap组件 导航 使用基础案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- Python做的第一个小项目-模拟登陆
1. 用户输入帐号密码进行登陆 2. 用户信息保存在文件内 3. 用户密码输入错误三次后锁定用户 主要采用循环语句和条件语句进行程序流程的控制,加入文件的读写操作 while True: choice ...
- ueditor 和 umeditor 粘贴过滤问题
最近遇到需要将WORD WPS等复制的带有格式的内容粘贴到富文本编辑器里面去掉冗余的HTML,只保留最有用的部分. 第一步肯定是先查官方文档了. http://fex.baidu.com/uedito ...
- Scrum Meeting Alpha - 7
Scrum Meeting Alpha - 7 NewTeam 2017/11/1 地点:新主楼F座二楼 任务反馈 团队成员 完成任务 计划任务 安万贺 登录部分的API仍存在问题 完成登陆部分的AP ...
- NoSQL:redis缓存数据库
一 Redis介绍 Redis和Memcached类似,也属于key-value nosql 数据库 Redis官网redis.io, 当前最新稳定版4.0.1 和Memcached类似,它支持存储的 ...
- map的常用方法
1.头文件: #include<map> 2.定义: map<string,int>Map; 或: typedef map<string,int> MAP; MAP ...
- Ubuntu初始化设置
1.安装chrome sudo apt-get install chromium-browser sudo apt-get install pepperflashplugin-nonfree sudo ...
- 读lodash源码之从slice看稀疏数组与密集数组
卑鄙是卑鄙者的通行证,高尚是高尚者的墓志铭. --北岛<回答> 看北岛就是从这两句诗开始的,高尚者已死,只剩卑鄙者在世间横行. 本文为读 lodash 源码的第一篇,后续文章会更新到这个仓 ...
- C语言系列之printf和%12d的用法(三)
看C语言程序的时候,往往会遇到printf函数输出,在此,我想总结一下printf的一般用法以及%12d是什么意思 printf函数的一般格式为 printf(格式控制,输出列表): 例如: prin ...
- 微信JS-SDK 选取手机照片并进行上传
项目中遇到需要选取照片上传的需求,因为网页运行在微信的浏览器里面,所以用微信的 js-sdk 提供的选取照片功能,来进行项目开发.实际开发中需要用到微信web开发者工具,详细参考链接:https:// ...