HDU 5504 GT and sequence 模拟

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5504

思路:模拟

代码:

  1 #include<stdio.h>//------杭电5504
  2 #include<algorithm>
  3 #include<math.h>
  4 #include<iostream>
  5 using namespace std;
  6 int comp(const void*a, const void*b)
  7 {
  8     return *(int*)a - *(int*)b;
  9 }
 10 int main()
 11 {
 12     int t;
 13     scanf("%d", &t);
 14     while (t)
 15     {
 16         __int64 *a;
 17         int n;
 18         int count1 = 0;//记录负数
 19         int count2 = 0;//记录0
 20         int count3 = 0;//记录正数
 21         int i;
 22         __int64 result = 1;
 23         __int64 maxx = -0x3f3f3f3f;
 24         scanf("%d", &n);
 25         a = (__int64*)malloc(n*sizeof(__int64));
 26         for (i = 0; i < n; i++)
 27         {
 28             scanf("%I64d", (a + i));
 29             if (a[i] < 0)
 30             {
 31                 count1++;
 32                 maxx = max(maxx, a[i]);
 33             }
 34             else if (a[i]>0)
 35                 count3++;
 36             else
 37                 count2++;
 38         }
 39         qsort(a, n, sizeof(__int64), comp);//对数组排序
 40         if (count1 == 0)//没有负数
 41         {
 42             if (count3 != 0)
 43             {
 44                 if (count2 == 0)
 45                     i = 0;
 46                 else
 47                     i = count2;
 48                 for (i; i < n; i++)
 49                 {
 50                     result *= a[i];
 51                 }
 52             }
 53             else if (count3 == 0)//无正数
 54             {
 55                 result = 0;
 56             }
 57         }
 58         else if (count1 == 1)//只有一个负数
 59         {
 60             if (count3 != 0)
 61             {
 62                 if (count2 == 0)
 63                     i = 1;
 64                 else
 65                     i = count2 + count1;
 66                 for (i; i < n; i++)
 67                 {
 68                     result *= a[i];
 69                 }
 70             }
 71             else if (count3 == 0)//无正数
 72             {
 73                 if (count2 == 0)
 74                     result *= a[0];
 75                 else
 76                     result = 0;
 77             }
 78         }
 79         else if (count1 % 2 == 0)//偶数个负数
 80         {
 81             for (i = 0; i < count1; i++)
 82             {
 83                 result *= a[i];
 84             }
 85             if (count3 != 0)
 86             {
 87                 if (count2 == 0)
 88                     i = count1;
 89                 else
 90                     i = count2 + count1;
 91                 for (i; i < n; i++)
 92                 {
 93                     result *= a[i];
 94                 }
 95             }
 96             else if (count3 == 0)//无正数,值保留
 97             {
 98                 result = result;
 99             }
100         }
101         else if (count1 % 2 != 0)//奇数个负数
102         {
103             for (i = 0; i < count1-1; i++)
104             {
105                 result *= a[i];
106             }
107             if (count3 != 0)
108             {
109                 if (count2 == 0)
110                     i = count1;
111                 else
112                     i = count2 + count1;
113                 for (i; i < n; i++)
114                 {
115                     result *= a[i];
116                 }
117             }
118             else if (count3 == 0)//无正数,值保留
119             {
120                 result = result;
121             }
122         }
123         printf("%I64d\n", result);
124         t--;
125     }
126     return 0;
127 }
View Code

 

posted @ 2017-11-03 01:43  lemonsbiscuit  阅读(126)  评论(0编辑  收藏  举报