hdu5504 GT and sequence

本文深入探讨了在解决复杂算法问题时如何运用特判技巧,通过实例展示了特判在不同场景下的作用,旨在帮助读者提升算法解决能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不是小水题。。。重点在特判。。。

#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <math.h>
#define LL long long
#define maxn 65

using namespace std;

LL a[maxn];

int main()
{
    int T, N;
    while (~scanf("%d", &T)) {
        while (T--) {
            scanf("%d", &N);
            int f = 0, z = 0;
            LL ans = 1;
            for (int i = 1; i <= N; i++) {
                LL tem;
                scanf("%I64d", &tem);
                if (tem == 0) {
                    z++;
                } else if (tem < 0) {
                    a[++f] = tem;
                } else {
                    ans *= tem;
                }
            }
            if (f + z == N) {
                if (f == 0) {
                    puts("0");
                    continue;
                }
                if (f == 1) {
                    if (z == 0) {
                        printf("%I64d\n", a[1]);
                    } else {
                        puts("0");
                    }
                    continue;
                }
            }
            sort(a + 1, a + 1 + f);
            for (int i = 1; i <= f / 2 * 2; i++) {
                ans *= a[i];
            }
            printf("%I64d\n", ans);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值