暑期集训之Array
来源:互联网 发布:vue.js 路由 编辑:程序博客网 时间:2024/06/06 16:25
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.
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.
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.
3-1 2 0
1 -11 21 0
4-1 -2 -3 0
1 -12 -3 -21 0
这道题说实话,虽然是在栈和队列的专题里面,但就像0和5那道题一样,用sort就解决了,或许有用到栈或者队列做,但我没想到怎么做,所以还是按着简单的方法写了。
其实,说白了这道题就是分情况的题,题目说了,如果结果不唯一,那么输出一种情况就好了,所以根据这一情况,我们可以输出最简单的那种情况,不过我还是先把这道题要分的情况都写出来把:
1.负数的个数为奇数,不存在偶数
2.负数的个数为奇数,存在偶数
3.负数的个数为偶数,不存在偶数
4.负数的个数为偶数,存在偶数
由题意可知,我们最后输出的是相乘小于零,大于零,和等于零这三种情况,所以偶数数量是奇数还是偶数对结果没有影响,所以只考虑负数的个数就好,其实说到这里应该也就知道是啥意思了,也就不多说啥了,具体代码如下,很简单,唯一的难点就是情况有点多,只要分好情况,再一一讨论就可以AC了,话不多说,看代码吧:
#include<stdio.h>#include<string.h>#include<iostream>#include<vector>#include<stack>#include<queue>#include<algorithm>using namespace std;int main(){int n;int a[105];while(scanf("%d",&n)!=EOF){ int zheng=0,fu=0,shu=n;for(int i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n); for(int i=0;i<n;i++){if(a[i]<0)fu++;else if(a[i]>0)zheng++;} if(fu==1) { printf("1 %d\n",a[0]); printf("%d",n-2); for(int i=1;zheng>0;i++){ if(a[i]!=0){printf(" %d",a[i]);zheng--;}if(zheng==0)printf("\n");}printf("1 0\n");}else if((fu-1)%2==0){ printf("1 %d\n",a[0]); printf("%d",n-2);for(int i=1;i<n;i++){if(a[i]==0)continue;else{printf(" %d",a[i]); shu--;}if(shu==2)printf("\n");}printf("1 0\n");}else if(zheng==0&&(fu-1)%2!=0){ printf("1 %d\n",a[0]); printf("%d",n-3);for(int i=1;i<n-2;i++)printf(" %d",a[i]);printf("\n");printf("2 %d 0\n",a[n-2]);}else if(zheng!=0&&(fu-1)%2!=0){printf("1 %d\n",a[0]); printf("%d",n-3);for(int i=2;i<n;i++){if(a[i]==0)continue;else{printf(" %d",a[i]);shu--;}if(shu==3)printf("\n");}printf("2 %d 0\n",a[1]);}}return 0;}
- 暑期集训之Array
- 暑期集训之最小公倍数
- 暑期集训之cake
- 暑期集训之ZOJ
- 暑期集训之哈夫曼树
- 暑期集训之Subsequence
- 暑期集训之pie
- 暑期集训之Highways
- 暑期集训之感
- 暑期集训之最大公约数问题
- 暑期集训之立方求和
- 暑期集训之EXCEL排序
- 暑期集训之稳定排序
- 暑期集训之寻找大富翁
- 暑期集训之阶乘之和
- 暑期集训
- 暑期集训之美素数问题
- 暑期集训之一般素数的判断
- qt发布windows qpp
- sass用法 指南
- [leetcode]264. Ugly Number II
- 【51Nod
- WdatePicker.js时间日期插件
- 暑期集训之Array
- tesorflow 学习笔记1 线性回归预测
- Android性能优化(九)之不可忽视的电量
- 用java实现对MySql数据库中数据的读取、插入、修改和删除
- jquery事件on对于未来元素失效的解决办法
- 关于linux下的普通文本在windows下缺少换行问题
- lua语言学习七字符串
- 详解cve-2010-2553
- C#第一天的作业