zoukankan      html  css  js  c++  java
  • hdu 5504 GT and sequence (水题)

    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

       题意:给出一个数列,从数列中选出一些数,使这些数的乘积最大。

       解法:给数列排序。从小于零中选出最小的偶数个数,零不要选,大于零的全选就可以了。一些特殊情况就特殊考虑就行了。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 int main()
     6 {
     7     long long a[100];
     8     long long t,n,i,cut;
     9     scanf("%I64d",&t);
    10     while (t--)
    11     {
    12         scanf("%I64d",&n);
    13         for (i=0;i<n;i++) scanf("%I64d",&a[i]);
    14         sort(a,a+n);
    15         if (a[0]==0&&a[n-1]==0)
    16         {
    17             printf("0
    ");
    18             continue;
    19         }
    20         if (n==1)
    21         {
    22             printf("%I64d
    ",a[0]);
    23             continue;
    24         }
    25         if (a[0]<0&&a[1]==0&&a[n-1]==0)
    26         {
    27             printf("0
    ");
    28             continue;
    29         }
    30         long long ans=1;
    31         for (i=0;i<n;i++)
    32         {
    33             if (a[i]==0) continue;
    34             if (a[i]<0&&i%2==1) ans*=a[i];
    35             if (a[i]<0&&i!=n-1&&i%2==0&&a[i+1]<0) ans*=a[i];
    36             if (a[i]>0) ans*=a[i];
    37         }
    38         printf("%I64d
    ",ans);
    39     }
    40 }
  • 相关阅读:
    点击图片显示原图
    SQL判断语句
    窗口淡入淡出效果
    判断两段时间之间的时间差
    软件行业发展趋势
    VSS客户端不能访问问题“unable to open user login file\\服务器项目管理目录\data\logedin\用户名.log ”
    鑫哥儿子顺利降生了!
    面向对象原则之单一职责原则实现
    PHP编码,乱码问题
    泛型中的default(T)
  • 原文地址:https://www.cnblogs.com/pblr/p/4889235.html
Copyright © 2011-2022 走看看