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 2^63−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
Source
BestCoder Round #60
题意:给出一串数(1≤N≤62) ,问从中取出一系列的数(至少一个),,使得这些数的乘积最大。
解法:首先找出给出的串中正数与负数。。以及0的个数。
1:当这一串数只有一个时,无论这个数是正负还是0都要取这个数。
2:当没有正数时,剩下的负数所组成的最大数(见3),若有0,则为0,没有这个这个做大数。
3:负数组成的最大数,,当负数个数为奇数时,取偶数个且最大那个不取,当为偶数数,全取。
4:结果为负数组成的最大数乘所有正数。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int T_T;
int n;
// int a[100];
long long fu[100];
long long zheng[100];
int pp,qq,cnt;
long long out,w;
scanf("%d",&T_T);
while(T_T--)
{
pp=0;//zheng
qq=0; //fu
cnt=0; //0
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%I64d",&w);
if(w>0) zheng[pp++]=w;
else if(w<0) fu[qq++]=w;
else cnt++;
}
sort(fu,fu+qq);
if(pp==0&&qq==0&&cnt!=0) out=0;
else if(pp==0&&qq==1&&cnt==0) out=fu[0];
else if(pp==1&&qq==0&&cnt==0) out=zheng[0];
else if(pp==0&&qq==1&&cnt!=0) out=0;
else
{
out=1;
for(int i=0;i<pp;i++) out=out*zheng[i];
if(qq%2==0)
{
for(int i=0;i<qq;i++) out=out*fu[i];
}
else
{
for(int i=0;i<qq-1;i++) out=out*fu[i];
}
}
printf("%I64d\n",out);
}
return 0;
}
/*********
考虑只有一个数,和有0的情况
最大就是:最大偶数个负数*所有正数
*********/