求子数组最大乘积

#include <iostream>
#include   <stdlib.h> 
#include   <stdio.h> 
using namespace  std;

int findMaxProduct(int *a,int n)
{
	
	if ( a == NULL )
		{
			cout<<"input illegal"<<endl;
			return 0;
		}
	if ( n == 1 )
		return a[0];
	int maxP = 1;
	int minN = 1;
	int r = 1;	
	for(int i=0;i<n;i++)
	{
		if (a[i] > 0)
		{
			cout<<"大于0"<<" ";
			maxP *= a[i];//
			minN = min(minN*a[i],1);/
		}
		else if (a[i] == 0)
		{
			cout<<"等于0"<<" ";
			maxP = 1;
			minN = 1;
		}
		else //a[i]<0
		{
			cout<<"小于0"<<" ";
			int tmp = maxP;
			maxP = max(minN*a[i],1);//
			minN	= tmp*a[i]; 
		}
		r = max(r,maxP);
	}
	cout<<endl;
	return r;
	}
int main()
{
	int n = 27;
	int a[] = {23,22,3,29,14,1,19,-19,-7,-11,-21,-15,1,1,2,13,25,-9,22,-19,-22,-14,26,-1,30,-6,4,27,-19,-16,0,22,-2,21,-21,-8,-4,13,-14,-14,-19,-15,12,12,30,11,-22,17,25,5,-14,0,-28,-1,29,-16,-26,-26,-12,-26,28,22,4,-2,-24,-12,19,24,26,15,29,-13,-27,-5,9,-11,-5,4,-14,-14,3,-26,-20,-27,25,22,11,-9,-17,-17,12,15,3,-9,-29,12,30,0,-14,-22,-21,4,9,-18,-23,17,-13,-8,-16,21,19,-30,-21,-21,-18,19,-3,5,28,-27,-24,-21,-10,16,-23,-2,-15,-11,20,-18,-25,-4,27,7,1,9,-7,27,25,26,-23,-16,17,27,19,-16,-15,-26,23,11,29,11,-9,-3,7,15,13,-26,-20,9,-18,17,-19,-15,11,-20,-5,-3,20,10,-18,-14,19,-21,-11,-27,21,-5,-30,-11,5,25,-7,23,-30,-21,-8,-5,-19,-16,14,-26,-6,-13,10,-30,-8,-13,-15,-16,22,26,-9,4,25,-26,26,-11,8,18,25,22,-13,14,13,-6,10,18,-10,-23,15,12,10,26,12,12,-19,29,-24,-19,-1,-17,1,-1,-22,-5,18,2,3,-24,-25,-15,-22,8,14,-2,28,21,22,-18,-8,-25,-27,7,-7,24,-18,20,-18,2,-25,-23,14,9,5,26,30,-26,-24,14,-16,14,26,9,0,5,-4,-13,-20,30,-14,6,9,18,23,-9,13,0,2,11,-5,7,8,20,13,8,-9,-13,-26,-27};
	int result = findMaxProduct(a,n);
	cout<<"长度为 "<<n<<" 的子数组最大乘积 "<<" result ="<<result<<endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值