H - fluffy hair HDU - 5504

Give you a sequence of N integers.

You should choose some numbers (at least one), and make their product as possible.

It guarantees that any product of the number of your choice in the initial sequence is not greater than the absolute value of 263-1. Input in the first row have a number T (represents the number of samples).

For each test, the first line has a number N, there are N numbers next line.

1≤T≤1000 1≤N≤62

You'd better print carriage returns in the last row

You'd better not print at the end of each row of spaces Output For each test case, output the answer.

Sample Input
1
3
1 2 3
Sample Output
6

Thinking

  • Said the sentence: Note that there is no zero, if 0, 0 Note that there are multiple, but also pay attention to the situation only when there is a negative 0 ( hey I gnaw so dish )

Code

#include<iostream>
#include<cmath>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int read()
{
    int res=0;char ch=0;
    while (!isdigit(ch))ch=getchar();
    while (isdigit(ch))res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
    return res;
}
ll ar[100];

int main()
{
    /* freopen("A.txt","r",stdin); */
    int t;
    scanf("%d", &t);
    while(t --)
    {
        int cnt = 0;
        ll n;
        scanf("%lld", &n);
        ll val;
        ll ans = 1;
        bool have_z = 0;
        bool have_0 = 0;
        int have_f = 0;
        for(int i = 1; i <= n; i ++)
        {
            scanf("%lld", &val);
            if(val > 0)
                ans *= val, have_z = 1;
            else if(val < 0)
                ar[++ cnt] = val, have_f ++;
            else
                have_0 = 1;
        }

        if(n == 1)
        {
            printf("%lld\n", val);
            continue;
        }
        if(! have_z && have_0 && have_f <= 1)
        {
            printf("0\n");
            continue;
        }
        sort(ar + 1, ar + 1 + cnt);

        if(cnt % 2)
        {
            for(int i = 1; i < cnt; i ++)
                ans *= ar[i];
        }
        else
        {
            for(int i = 1; i <= cnt; i ++)
                ans *= ar[i];
        }

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

    return 0;
}

Guess you like

Origin www.cnblogs.com/lql-nyist/p/12636757.html