GT and sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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
Source
/************************************************************************/
附上该题对应的中文题
GT and sequence
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
问题描述
给出N个整数。你要选择至少一个数,使得你选的数的乘积最大。 保证任意选一些数相乘的绝对值都不会大于263−1。
输入描述
第一行读入一个数T表示数据组数。 对于每组数据: 第一行是一个数N,第二行是N个整数。 1≤T≤1000 1≤N≤62 hack时建议输出最后一行的行末回车;每一行的结尾不要输出空格。
输出描述
对于每组数据,输出一个数表示最大的乘积。
输入样例
1 3 1 2 3
输出样例
6
/****************************************************/
出题人的解题思路:
1001 GT and sequence
注意先特判0的情况:如果读入的数据有0,那么去掉所有的0且最后答案和0取一个max。
剩下的正数显然全部乘起来比较优。
对于负数的话,如果个数是奇数个我们就去掉绝对值最小的那一个,然后全部乘起来即可。
这题就不多说了,就是坑比较多,出题人挖了这么多坑,只能由我们自己来填,与其说得再多,不如提供几组数据测试吧
INPUT
1
1
0
0
OUTPUT
0
INPUT
1
INPUT
1
1
-1
-1
OUTPUT
-1
INPUT
1
INPUT
1
2
1 -1
1 -1
OUTPUT
1
INPUT
1
INPUT
1
3
0 0 0
0 0 0
OUTPUT
0
INPUT
1
INPUT
1
3
-1 0 0
-1 0 0
OUTPUT
0
INPUT
1
INPUT
1
3
-1 0 2
-1 0 2
OUTPUT
2
INPUT
1
INPUT
1
5
-2 -3 2 4 1
-2 -3 2 4 1
OUTPUT
48
INPUT
1
INPUT
1
5
-2 -3 -5 4 2
-2 -3 -5 4 2
OUTPUT
120
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
#define inf 10000000000
using namespace std;
const int N = 100;
const int mod = 2009;
__int64 s[N];
int main()
{
int t,n,i,k,flag;
__int64 ans,Max;
scanf("%d",&t);
while(t--)
{
ans=flag=k=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%I64d",&s[i]);
if(s[i]==0)
{
flag++;
continue;
}
if(s[i]<0)
{
if(k)
Max=max(Max,s[i]);
else
Max=s[i];
k++;
}
if(i==flag)
ans=s[i];
else
ans*=s[i];
}
if(k%2&&n!=1&&!(k==1&&flag==n-k))
ans/=Max;
if(flag>0&&(ans<=0||n==1))
ans=0;
printf("%I64d\n",ans);
}
return 0;
}
菜鸟成长记

本文解析了GTandsequence题目,给出了高效的解题思路及代码实现。面对整数序列,如何选择元素使其乘积最大化?文章通过具体示例阐述了解决方案,并提供了详细的程序代码。
1057

被折叠的 条评论
为什么被折叠?



