Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold:
- The product of all numbers in the first set is less than zero ( < 0).
- The product of all numbers in the second set is greater than zero ( > 0).
- The product of all numbers in the third set is equal to zero.
- Each number from the initial array must occur in exactly one set.
Help Vitaly. Divide the given array.
Input
The first line of the input contains integer n(3 ≤ n ≤ 100). The second line contains n space-separated distinct integers a1, a2, ..., an(|ai| ≤ 103) — the array elements.
Output
In the first line print integer n1(n1 > 0) — the number of elements in the first set. Then print n1 numbers — the elements that got to the first set.
In the next line print integer n2(n2 > 0) — the number of elements in the second set. Then print n2 numbers — the elements that got to the second set.
In the next line print integer n3(n3 > 0) — the number of elements in the third set. Then print n3 numbers — the elements that got to the third set.
The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them.
Sample Input
3
-1 2 0
1 -1
1 2
1 0
4
-1 -2 -3 0
1 -1
2 -3 -2
1 0
解题说明:此题的要求是对一列数进行分组,要求分到第一组的数字乘积小于0,第二组数字的乘积大于0,第三组数字的乘积为0. 首先对数组进行排序,把最小的数字放到第一组中【输入肯定能保证至少存在一个负数】,然后判断数组中有没有大于0的数字,如果有的话,就把最小的那个大于0的数字放到第二组中,然后把其他所有数字放到第三组【题目中也肯定保证存在一个0】,如果没有大于0的数字,就把两个负数放到第二组【乘积大于0】,其他数字放到第三组。
代码:
#include<stdio.h> #include<algorithm> using namespace std; int main() { int n,a[100],k; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&a[i]); } sort(a,a+n); printf("1 %d\n",a[0]); k=0; for(int i=0;i<n;i++) { if(a[i]>0) { printf("1 %d\n",a[i]); k=a[i]; break; }//判断数组中有没有大于0的数字,如果有的话,就把最小的那个大于0的数字放到第二组中 } if(k>0) { printf("%d",n-2); for(int i=1;i<n;i++) { if(a[i]!=k) { printf(" %d",a[i]); } } printf("\n");//把其他所有数字放到第三组【题目中也肯定保证存在一个0】 } else { printf("2 %d %d\n",a[1],a[2]);//如果没有大于0的数字,就把两个负数放到第二组【乘积大于0】 printf("%d",n-3); for(int i=3;i<n;i++) { printf(" %d",a[i]);//其他数字放到第三组 } printf("\n"); } return 0; }