首页 > 其他 > 详细

hdu 5504 GT and sequence (水题)

时间:2015-10-18 12:39:07      阅读:252      评论:0      收藏:0      [点我收藏+]
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 2631.
 

 

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.
1T1000 1N62
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 }

 

hdu 5504 GT and sequence (水题)

原文:http://www.cnblogs.com/pblr/p/4889235.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!