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;
}
#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……