题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5504
GT and sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2223 Accepted Submission(s):
510
Problem Description
You are given a sequence of N
integers.
You should choose some numbers(at least one),and make the
product of them as big as possible.
It guaranteed that **the absolute
value of** any product of the numbers you choose in the initial sequence will
not bigger than 263−1
.
Input
In the first line there is a number T
(test numbers).
For each test,in the first line there is a number N
,and in the next line there are N
numbers.
1≤T≤1000
1≤N≤62
You'd better print the enter in the last line when you hack
others.
You'd better not print space in the last of each line when you
hack others.
Output
For each test case,output the answer.
Sample Input
1
3
1 2 3
Sample Output
6
#include<stdio.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#define LL long long
#define MAX 1100
using namespace std;
int main()
{
LL t,n,m,j,i,k;
LL x,a1,b1,c;
LL s[1100],a[MAX],b[MAX];
scanf("%lld",&t);
while(t--)
{
scanf("%lld",&m);
a1=b1=c=0;
for(i=0;i<m;i++)
{
scanf("%lld",&s[i]);
if(s[i]<0)
a[a1++]=s[i];
else if(s[i]>0)
b[b1++]=s[i];
else
c++;
}
LL sum1=1;
LL sum2=1;
if(c==m)//只输入0
{
printf("0\n");
continue;
}
if(m==1)//只输入一个数
{
printf("%lld\n",s[0]);
continue;
}
sort(a,a+a1);
sort(b,b+b1);
for(i=0;i<b1;i++)//正数和
sum1*=b[i]; if(a1%2==0)//偶数个负数的话,所有负数乘
{
for(i=0;i<a1;i++)
sum2*=a[i];
}
else //奇数个负数,最大的负数不乘
for(i=0;i<a1-1;i++)
sum2*=a[i]; if(c!=0&&b1==0&&a1==1)//输入1个负数和0
{
printf("0\n");
continue;
}
printf("%lld\n",sum1*sum2);
}
return 0;
}