最大乘积 Maximun Product
最大乘积
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/B
题意:
输入n个元素组成的序列s,你需要找出一个乘积最大的连续子序列。如果这个最大的乘积不是正数,应输出0.
注意格式。
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.
分析:
设置x为最大值y为最小值 ,在枚举更新时,如果a[i]为正,则对应跟新,
a[i]为0时,当前最大值,最小值置零,a[i]小于0时,最大的值乘以a[i]后变成最小,
相反最小变成最大。
#include<iostream>
using namespace std;
int main()
{
int i,n,a[],count=;
while(cin>>n)
{
long long max=,x=,y=,z; //乘积最大可以为10^18所以防止溢出用long long
for(i=;i<n;i++)
cin>>a[i];
for(i=;i<n;i++)
{
if(a[i]==) x=y=;
else if(a[i]>)
{ x=x*a[i];
y=y*a[i];
if(x==) x=a[i];
}else if(a[i]<) //小于0进行交叉相乘
{
z=x*a[i];
x=y*a[i];
y=z;
if(y==) y=a[i];
}
if(max<x&&a[i])
max=x;
}
printf("Case #%d: The maximum product is %lld.\n\n",count++,max);
}
return ;
}
最大乘积 Maximun Product的更多相关文章
- 暴力求解——最大乘积 Maximum Product,UVa 11059
最大乘积 Maximum Product 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/B 解题思路 ...
- [Swift]LeetCode628. 三个数的最大乘积 | Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- 枚举专项练习_Uva725(Division)_Uva11059(Maximun Product)
//Uva725 #include <iostream> #include <cstring> #include <cstdlib> #include <cs ...
- leecode 每日解题思路 152 Maximun Product Subarray
问题描述: 问题链接:152 Maximum Product Subarray 在经典的算法解析中, 有关的分治和动态规划的,经典题型之一就是求最大子段和, 这道题就是他的变形:求最大子段积; 这个问 ...
- 最大乘积(Maximum Product,UVA 11059)
Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...
- Maximun product
Given a sequence of integers S = {S1, S2, ..., Sn}, you shoulddetermine what is the value of the max ...
- LeetCode 238. 除自身以外数组的乘积( Product of Array Except Self)
题目描述 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输 ...
- 【LeetCode】1464. 数组中两元素的最大乘积 Maximum Product of Two Elements in an Array (Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找最大次大 日期 题目地址:https://le ...
- [Swift]LeetCode318. 最大单词长度乘积 | Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
随机推荐
- php调用phpqrcode.php生成二维码
下载phpqrcode.php 下载地址: http://files.cnblogs.com/files/qhorse/phpqrcode.rar qrcode.php文件: <?php inc ...
- 使用html5的离线缓存技术
突然想用html5的离线缓存,但是一直没有成功,在各种群里问发现很多人都没什么经验,最终终于在各种论坛找到解决方案了.下面就简单记录一下相关情况. 一.离线缓存的优点 我们都知道离线缓存主要是用来减少 ...
- Intent.putExtra()传递Object对象或者ArrayList<Object> (转)
Intent传递基本类型相信大家都十分熟悉,如何传递Object对象或者ArrayList<Object>对象呢? 可以通过: (1)public Intent putExtra (Str ...
- POJ1201 Intervals差分约束系统(最短路)
Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...
- js实现冒泡排序
冒泡排序 冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法. 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到 ...
- Flume内存溢出错误
java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:) at java.lang.Ab ...
- 【Oracle】Oracle 11g 64位安装完后,ora-12541错误和ora-12514错误的解决
问题描述: 干净的windows2008 64位服务器上安装 oracle 11g R2 64bit服务端,安装完后,NetManager中默认的主机名为localhost,可以测试通过.但是无法在别 ...
- Modify a Stored Procedure using SQL Server Management Studio
In Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand ...
- BZOJ4624 : 农场种植
设$A[i][j]=[a[i][j]=G],B[i][j]=[b[i][j]=L]$,枚举右下角,则对应$(A-B)^2$的和就是匹配成功的格子数. $(a-b)^2=a^2+b^2-2ab$,将矩阵 ...
- CentOS增加swap分区
使用dd命令创建一个swap分区 [root@localhost Desktop]#dd if=/dev/zero of=/home/swap bs=1024 count=1048576 count的 ...