HDOJ 5504-GT and sequence

秒速五厘米 2022-08-20 02:18 132阅读 0赞

GT and sequence

Accepts: 95

Submissions: 1467

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 65536/65536 K (Java/Others)

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 2 63 −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

Copy

  1. 1
  2. 3
  3. 1 2 3

Sample Output

Copy

  1. 6
  2. 解题思路:
  3. 题目大意很简单就是从一串数中挑出一些数,将其相乘,所得的结果是最大值。注意这里将包含负数,所以将分开讨论,对于正数在输入时就要累成处理,对于负数要注意负数的个数,和0的个数。
  4. 参考数据:
  5. 20
  6. 2
  7. -1 0
  8. 0
  9. 3
  10. -2 3 0
  11. 3
  12. 5
  13. -2 3 -2 3 -2
  14. 36
  15. 5
  16. 0 0 0 0 0
  17. 0
  18. 1
  19. 0
  20. 0
  21. 3
  22. -2 -9 3
  23. 54
  24. 剩下要注就是__int64的定义。
  25. #include<stdio.h>
  26. #include<string.h>
  27. #include<algorithm>
  28. __int64 map[2000];//负数
  29. __int64 map1[2000];//正数
  30. __int64 uz,uf,u0;
  31. using namespace std;
  32. bool cmp(__int64 x,__int64 y)
  33. {
  34. return x<y;
  35. }
  36. int main()
  37. {
  38. int T;
  39. scanf("%d",&T);
  40. while(T--)
  41. {
  42. __int64 hh=1,hu=1;
  43. __int64 N,uu;
  44. scanf("%I64d",&N);
  45. __int64 i,j;
  46. uz=uf=u0=0;
  47. for(i=0;i<N;i++)
  48. {
  49. scanf("%I64d",&uu);
  50. if(uu==0)
  51. u0++;
  52. if(uu<0)
  53. {
  54. map[uf]=uu*(-1);
  55. hu=hu*(uu*(-1));
  56. uf++;
  57. }
  58. if(uu>0)
  59. {
  60. map1[uz]=uu;
  61. hh*=uu;
  62. uz++;
  63. }
  64. }
  65. sort(map,map+uf,cmp);
  66. if(uz==0&&u0==0&&uf>0)
  67. {
  68. if(uf%2==0)
  69. {
  70. printf("%I64d\n",hu);
  71. }
  72. else
  73. {
  74. if(uf==1)
  75. {
  76. printf("%I64d\n",hu*(-1));
  77. }
  78. else
  79. printf("%I64d\n",hu/map[0]);
  80. }
  81. }
  82. if(uz==0&&u0>0&&uf==0)
  83. {
  84. printf("0\n");
  85. }
  86. if(uz==0&&u0>0&&uf>0)
  87. {
  88. if(uf%2==0)
  89. {
  90. printf("%I64d\n",hu);
  91. }
  92. else
  93. {
  94. if(uf==1)
  95. printf("0\n");
  96. else
  97. printf("%I64d\n",hu/map[0]);
  98. }
  99. }
  100. if(uz>0&&u0==0&&uf==0)
  101. {
  102. printf("%I64d\n",hh);
  103. }
  104. if(uz>0&&u0==0&&uf>0)
  105. {
  106. if(uf%2==0)
  107. {
  108. printf("%I64d\n",hu*hh);
  109. }
  110. else
  111. {
  112. printf("%I64d\n",hh*(hu/map[0]));
  113. }
  114. }
  115. if(uz>0&&u0>0&&uf==0)
  116. {
  117. printf("%I64d\n",hh);
  118. }
  119. if(uz>0&&u0>0&&uf>0)
  120. {
  121. if(uf%2==0)
  122. {
  123. printf("%I64d\n",hu*hh);
  124. }
  125. else
  126. {
  127. printf("%I64d\n",hh*(hu/map[0]));
  128. }
  129. }
  130. }
  131. return 0;
  132. }

发表评论

表情:
评论列表 (有 0 条评论,132人围观)

还没有评论,来说两句吧...

相关阅读