hdu5504

GT and sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1321    Accepted Submission(s): 308


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
 
一开始看这道题,看成了让选几个数,使得这几个数的乘积是不大于2^63-1的,并且是最大的,再读一遍,才发现是题中保证,任意的数的乘积不大于2^63-1,这样这道题就变得简单多了。但是如果要注意一些特殊情况,比如说,本来如果有0的话是绝对不选的,但是如果只有0的话,就只能选0,如果只有一个负数,剩下的全是0的话,那么也只能选0,如果只有一个负数的话,那么就只能选择负数,剩下的情况中,答案及时,所有数的乘积除以最大的那个负数了。

(话说,《设计模式》中的模板方法和c++中的模板方法是一种东西吗?应该不是吧 敲打

已ac的代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define N 70

long long int num[N];

int main(){
    int t;
    int n;
    int znum;
    int fnum;
    long long int fmax;
    long long int ans;

    scanf("%d",&t);
    while(t--){
        ans=1;
        fnum=0;
        znum=0;
        scanf("%d",&n);

        for(int i=1;i<=n;i++){
            scanf("%lld",&num[i]);

            if(num[i]<0){
                if(fnum==0){
                    fmax=num[i];
                }
                else if(fmax<num[i]){
                    fmax=num[i];
                }

                fnum++;
            }
            if(num[i]!=0){
                ans=ans*num[i];
            }
            else{
                znum++;
            }
        }

        if(fnum%2){// 这个判断需谨慎考虑。
            if(fnum==1){
                if(fnum+znum==n&&znum!=0){
                    ans=0;
                }
                else if(fnum!=n&&fnum+znum!=n){
                    ans=ans/fmax;
                }
            }
            else{
                ans=ans/fmax;
            }
        }
        else if(n==znum){
            ans=0;
        }

        printf("%lld\n",ans);
    }

    return 0;
}


转载于:https://www.cnblogs.com/ahahah/p/4918163.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值