hdu5504

来源:互联网 发布:淘宝客推广教程全攻略 编辑:程序博客网 时间:2024/06/05 20:27

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 than2631.
 

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
131 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 70long 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;}


0 0