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 }
  • 相关阅读:
    定制选择范围的按钮RangeButton
    获取系统屏幕尺寸参数的类WxHxD
    解决UITableView在iOS7中UINavigationController里的顶部留白问题
    [翻译] INSSearchBar
    可以简易设置文字内边距的EdgeInsetsLabel
    [翻译] STAlertView
    keyWindow与delegate中Window的区别
    定制二选一按钮SwitchButton
    【转】Xcode真机调试初体验
    【转】Xcode真机测试could not find developer disk image解决方法
  • 原文地址:https://www.cnblogs.com/pblr/p/4889235.html
Copyright © 2011-2022 走看看