HDU-5504(逻辑if-else大水题)

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 263−1.

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.

1≤T≤1000
1≤N≤62

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


思路:

这题恶心我了一晚上,昨天实在是没做的出来,今天缓了一天,然后给A了

题目大意是让你找出所给的一串数字中能达到的最大乘积,有很多的细节需要考虑

关键是要想到0,想清楚哪几种情况会受到0存在的影响

只要有正数存在,不管负数和0存在与否,都不会影响按照“一般流程”下来的最后结果

正数不存在,那么又有两种小的分支:负数也不存在和负数存在个数的问题

(1)只有一个负数,这个时候还要考虑有没有0!!!

(2)没有负数,只有0,那结果就是0.

是的,只有这两种情况会受到0存在的影响,要单独拿出来处理,其他的情况走大流程就OK


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

int main()
{
    int T;
    __int64 t;
    scanf("%d",&T);
    while(T--)
    {
        __int64 ans = 1;
        __int64 a[65];
        int n;
        int q = 0,f = 0;
        scanf("%d",&n);
        for(int i = 1;i <= n;i++) {
            scanf("%I64d",&t);
            if(t > 0) ans *= t;
            else if(t == 0) f++;
            else a[++q] = t;
        }
        //把0会造成影响的两种特殊情况拿出来处理一下
        if(q+f == n) {
            if(!q) {
                printf("0\n");
                continue;
            }
            if(q == 1) {
                if(!f) {printf("%I64d\n",a[1]);continue;}
                if(f) {printf("0\n");continue;}
            }
        }
        //如果有正数存在
        if(q%2 == 0) {
            for(int i = 1;i <= q;i++)
                ans *= a[i];
            printf("%I64d\n",ans);
            continue;
        }
        else {
            sort(a+1,a+1+q);
            for(int i = 1;i <= q-1;i++)
                ans *= a[i];
            printf("%I64d\n",ans);
            continue;
        }
    }
)   return 0;
}
时间: 2024-08-29 08:37:09

HDU-5504(逻辑if-else大水题)的相关文章

PAT甲题题解-1101. Quick Sort (25)-大水题

快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着循环一次,统计出代码中的minnum和maxnum即可,注意最后一定要输出'\n',不然第三个测试会显示PE,格式错误. #include <iostream> #include <cstdio> #include <algorithm> #include <map&

hdu 1999 不可摸数 水题。

不可摸数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7966    Accepted Submission(s): 2024 Problem Description s(n)是正整数n的真因子之和,即小于n且整除n的因子和.例如s(12)=1+2+3+4+6=16.如果任何数m,s(m)都不等于n,则称n为不可摸数. Input 包

大水题(water)

题目描述dzy 定义一个 $n^2$ 位的数的生成矩阵 $A$ 为一个大小为 $n \times n$ 且 Aij 为这个数的第 $i \times n+j-n$ 位的矩阵.现在 dzy 有一个数 $n^2$ 位的数 k,他想知道所有小于等于 k 的数的 $n \times n$ 生成矩阵有多少种.(如果不足 $n^2$ 位则补前缀零)输入输出格式输入格式第一行一个数 $n$,第二行一个 $n^2$ 位的数 $k$输出格式仅一行表示答案,答案可能很大,你只需输出答案对 $10^9 + 7$ 取模

[补档]两个奇怪的大水题

导引 这是两道由OSU(貌似是一个我没有听说过的游戏)引申出的大水题(淼到不行啊喂),壹佰万行代码哦. T1 OSU! 题目 osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: 一共有n次操作,每次操作只有成功与失败之分,成功对应1,失败对应0,n次操作对应为1个长度为n的01串.在这个串中连续的 X个1可以贡献X^3 的分数,这x个1不能被其他连续的1所包含(也就是极长的一串1,具体见样例解释) 现在给出n,以及每个操作的成功率,请你输出期望分数,输出四舍五

HDU 4085 斯坦纳树模板题

Dig The Wells Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 971    Accepted Submission(s): 416 Problem Description You may all know the famous story "Three monks". Recently they find som

HDU 4280 Island Transport 网络流裸题

非递归版好像被卡掉了,其他2个板子都能过. #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<vector> using namespace std; #define ll int const int MAXN = 100100;//点数的最大值 const int MAXM = 400010;//边数的最大值 const in

HDU 4925 Apple Tree(模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 解题报告:给你n*m的土地,现在对每一块土地有两种操作,最多只能在每块土地上进行两种操作,第一种是种苹果树操作,第二种是施肥操作,种苹果树操作可以使得该块地 长出一个苹果,施肥操作可以使得与这块土地相邻的土地的苹果产量变为原来的两倍,问可以得到的最多的苹果数量是多少? 例如一个4*4的土地,用1表示在该土地上做第一种操作,0表示在该土地上做第二种操作,可以得到最多苹果的操作如下: 0 1 0

HDU Senior&#39;s Gun (水题)

题意:给n把枪,m个怪兽,每把枪可消灭1怪兽,并获得能量=枪的攻击力-怪兽的防御力.求如何射杀能获得最多能量?(不必杀光) 思路:用最大攻击力的枪杀防御力最小的怪兽明显可获得最大能量.如果每把枪都去射杀刚好1点能量都拿不到的怪物,那简直等于把枪全丢掉. 1 //#pragma comment(linker,"/STACK:102400000,102400000") 2 #include <iostream> 3 #include <stdio.h> 4 #inc

hdu 4970 Killing Monsters(简单题) 2014多校训练第9场

Killing Monsters                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Kingdom Rush is a popular TD game, in which you should b

HDU 4940 Destroy Transportation system 规律题

答案只有2种情况,所以ans = rand()%2; if(ans)puts("happy") else puts("unhappy"); == 想过无源汇的网络流,还是比较麻烦的,然后没往下想... 设s点集有一些点, 多加一个点一定是y增加比较快_(:зゝ∠)_ 然后设s点集只有一个点 #include <cstdio> #include <map> #include <set> #include <algorithm&