zoukankan      html  css  js  c++  java
  • 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 2631.
     
    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
    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
    ");
                    continue;
                }
                if(q == 1) {
                    if(!f) {printf("%I64d
    ",a[1]);continue;}
                    if(f) {printf("0
    ");continue;}
                }
            }
            //如果有正数存在 
            if(q%2 == 0) {
                for(int i = 1;i <= q;i++)
                    ans *= a[i];
                printf("%I64d
    ",ans);
                continue;
            }
            else {
                sort(a+1,a+1+q);
                for(int i = 1;i <= q-1;i++)
                    ans *= a[i];
                printf("%I64d
    ",ans);
                continue;
            }
        }
    )   return 0;
    }
  • 相关阅读:
    7、NFC技术:让Android自动运行程序
    6、Android中的NFC技术
    5、NFC概述
    Delphi XE7中开发安卓程序一些有用的帮助资源
    Delphi开发安卓程序的感受
    Tomcat绿色版启动"startup.bat"一闪问题的解决方法!
    Delphi判断字符串中是否包含汉字,并返回汉字位置
    Delphi的DLL里如何实现定时器功能?
    Delphi的DLL里如何实现定时器功能?
    VS2013如何添加LIb库及头文件的步骤
  • 原文地址:https://www.cnblogs.com/immortal-worm/p/4959269.html
Copyright © 2011-2022 走看看