GT and sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 219 Accepted Submission(s): 60
Problem Description
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.
Input
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
Recommend
hujie | We have carefully selected several similar problems for you:
5508
5507
5506
5505
5503
解析:用flag記錄有沒有0,如果有0,在之後的結果中與 0 取一個最大值。
直接排序,對于a[i],如果a[i]>0,那麼ans*=a[i];
如果a[i]==0,那麼不作處理;
如果a[i]<0并且a[i+1]<0,那麼ans*=a[i]*a[i+1],否則不作處理。
代碼:
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=62;
LL a[maxn+10];
int main()
{
freopen("1.in","r",stdin);
LL t,n,i,ans; bool flag;
scanf("%I64dd",&t);
while(t--)
{
scanf("%I64d",&n);
for(i=1;i<=n;i++)scanf("%I64d",&a[i]);
sort(a+1,a+n+1),flag=a[0]=0;
for(i=1;i<=n;i++)
{
if(a[i]<0 && (i+1<=n && a[i+1]<0))
a[++a[0]]=a[i],a[++a[0]]=a[i+1],i++;
if(a[i]>0)a[++a[0]]=a[i];
if(a[i]==0)flag=1;
}
if(a[0]==0)
{
if(flag)ans=0;
else ans=a[1];
}
else
for(ans=1,i=1;i<=a[0];i++)ans*=a[i];
printf("%I64d\n",ans);
}
return 0;
}