标签:ring continue 测试用例 else 空格 tchar input pac int
给你一个N个整数的序列。
你应该选择一些数字(至少一个),并使它们的乘积尽可能大。
它保证你在初始序列中选择的任何数的乘积的绝对值不会大于263?1。 Input 在第一行有一个数字T(表示样例数)。
对于每个测试,第一行有一个数字N,下一行有N个数字。
1≤T≤1000 1≤N≤62
你最好在最后一行打印回车
你最好不要在每行的最后打印空格 Output 对于每个测试用例,输出答案。
Sample Input
1
3
1 2 3
Sample Output
6
#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;
}
标签:ring continue 测试用例 else 空格 tchar input pac int
原文地址:https://www.cnblogs.com/lql-nyist/p/12636757.html