您的位置:首页 > 其它

A. Array

2016-06-14 18:21 211 查看
[align=center]A.Array[/align]Crawling in process...Crawling failedTime Limit:2000MSMemory Limit:262144KB 64bit IO Format:%I64d & %I64uSubmitStatusDescriptionVitaly has an array of n distinct integers. Vitaly wants to divide this array into threenon-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.InputThe first line of the input contains integern(3 ≤ n ≤ 100). The second line containsn space-separated distinct integersa1, a2, ..., an(|ai| ≤ 103)— the array elements.OutputIn the first line print integern1(n1 > 0) — the number of elements in the first set. Then printn1 numbers — the elements that got to the first set.In the next line print integern2(n2 > 0) — the number of elements in the second set. Then printn2 numbers — the elements that got to the second set.In the next line print integern3(n3 > 0) — the number of elements in the third set. Then printn3 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 InputInput
3
-1 2 0
Output
1 -1
1 2
1 0
Input
4
-1 -2 -3 0
Output
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;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  水题