Max product

FJNU.1150

Description
You are given N integer numbers. Choose thee of them which give maximum product.

Input
There may be several cases. In every case , the first line contains N (3 <= N <= 10E4). Then N integer numbers follow. Absolute value of each number less than 3000.It terminated when N=0.

Output
You should output the product of the three numbers for every case.

Sample Input
9
3 5 1 7 9 0 9 -3 10
0

Sample Output
810

My Program

#include < iostream >
#include
< algorithm >
#define  N 10000
using   namespace  std;

int  main()
{
    
int num[N];
    
long positive,negative;
    
long n,i,x;
    
while(cin>>n)
    
{
        
if(n==0)
            
break;
        positive
=negative=0;
        
for(i=0;i<n;i++)
        
{
            cin
>>num[i];
            
if(num[i]>0)
                positive
++;
            
else
                
if(num[i]<0)
                    negative
++;
        }

        sort(num,num
+n);
        x
=num[n-1]*num[n-2]*num[n-3];
        
if(negative>=2)
            
if(positive>0)
                
if(num[0]*num[1]*num[n-1]>x)
                    x
=num[0]*num[1]*num[n-1];
        cout
<<x<<endl;
    }

    
return 0;
}

YOYO's Note:
 根据正数与负数的数量,当有1个以上正数和2个以上负数时考察最小数的积是否大于最大数的积。
其他情况下直接输出最大的三个数的积就好。
原来10e4是10000……我一开始以为是100000000,浪费掉2次RE、1次CE……

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值