首页 > 其他 > 详细

hdu 5504 GT and sequence

时间:2015-12-05 00:17:36      阅读:114      评论:0      收藏:0      [点我收藏+]

题目链接: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 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.

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

  

hdu 5504 GT and sequence

原文:http://www.cnblogs.com/tonghao/p/5020719.html

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