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.
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.
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
题意:给出一个数列,从数列中选出一些数,使这些数的乘积最大。
解法:给数列排序。从小于零中选出最小的偶数个数,零不要选,大于零的全选就可以了。一些特殊情况就特殊考虑就行了。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int main() 6 { 7 long long a[100]; 8 long long t,n,i,cut; 9 scanf("%I64d",&t); 10 while (t--) 11 { 12 scanf("%I64d",&n); 13 for (i=0;i<n;i++) scanf("%I64d",&a[i]); 14 sort(a,a+n); 15 if (a[0]==0&&a[n-1]==0) 16 { 17 printf("0\n"); 18 continue; 19 } 20 if (n==1) 21 { 22 printf("%I64d\n",a[0]); 23 continue; 24 } 25 if (a[0]<0&&a[1]==0&&a[n-1]==0) 26 { 27 printf("0\n"); 28 continue; 29 } 30 long long ans=1; 31 for (i=0;i<n;i++) 32 { 33 if (a[i]==0) continue; 34 if (a[i]<0&&i%2==1) ans*=a[i]; 35 if (a[i]<0&&i!=n-1&&i%2==0&&a[i+1]<0) ans*=a[i]; 36 if (a[i]>0) ans*=a[i]; 37 } 38 printf("%I64d\n",ans); 39 } 40 }