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 0Output
1 -1 1 2 1 0Input
4 -1 -2 -3 0Output
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;}
相关文章推荐
- 【HDU 5366】The mook jong 详解
- HDU 5240 Exam (好水的题)
- 北大—1006——Biorhythms
- 时间计算(heaven.pas/cpp)
- 工作依赖(job.cpp/pas)
- 题目 英雄 (BFS)
- kmp 学习 hihocoder #1015
- HDU 1096 A+B for Input-Output Practice (VIII)
- HUST-1601 - Shepherd 暴力
- P1478
- P1035
- P1008 难度2.7
- 2020 绝对值排序
- 2021 发工资咯
- 2022 海选女主角
- 2024 C语言合法标识符
- 2025 查找最大元素
- 2026 首字母变大写
- 2027 统计元音
- 2028 Lowest Common Multiple Plus