【Codeforces】-300A- Array(分三组正负零)

来源:互联网 发布:linux w3m 图片 编辑:程序博客网 时间:2024/06/04 23:33

点击打开题目

A. Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. The product of all numbers in the first set is less than zero ( < 0).
  2. The product of all numbers in the second set is greater than zero ( > 0).
  3. The product of all numbers in the third set is equal to zero.
  4. 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.

Examples
input
3-1 2 0
output
1 -11 21 0
input
4-1 -2 -3 0
output
1 -12 -3 -21 0

题意:把给定的数分为三组,第一组积为负,第二组积为正,第三组为零。

开始把负数个数分奇偶讨论,写出的代码wa了说我用的数不是给定的,这就郁闷了好吧,然后换一种简单的。

#include<cstdio>#include<algorithm>using namespace std;bool cmp(int a,int b){return a<b;}int main(){int n;int a[110];int num=0;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);if(a[i]<0)num++;}sort(a+1,a+1+n,cmp);printf("1 %d\n",a[1]);//必存在,则先输出一个负数 if(a[n]>0)//若a[n]>0直接输出即为正,其他和零一起输出 {printf("1 %d\n",a[n]);printf("%d",n-2);for(int i=2;i<=n-1;i++)printf(" %d",a[i]);printf("\n");}else//a[n]不大于零,这输出2,3负数,其他和零输出 {printf("2 %d %d\n",a[2],a[3]);printf("%d",n-3);for(int i=4;i<=n;i++) printf(" %d",a[i]);printf("\n");}return 0;}

下面是莫名其面wa的代码!用的数不是给定的是什么鬼!!

#include<cstdio>#include<algorithm>using namespace std;bool cmp(int a,int b){return a<b;}int main(){int n;int a[110];int b=0;int c=0;int d=0;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);if(a[i]<0)b++;if(a[i]==0)c++;if(a[i]>0)d++;}sort(a+1,a+n+1,cmp);if(b%2==1){printf("1 %d\n",a[1]);//负数个数为奇,一个给第一组,剩偶数个 printf("%d ",b+d-1);if(d==0){for(int i=2;i<b;i++){printf("%d ",a[i]);}printf("%d\n",a[b]);}else{for(int i=2;i<n;i++){if(a[i]!=0)printf("%d ",a[i]);}printf("%d\n",a[n]);} printf("%d ",c);for(int i=1;i<c;i++)printf("0 ");printf("0\n");}else{printf("1 %d\n",a[1]);//负数个数为偶,一个给第一组,一个给零,剩偶数个 printf("%d ",b+d-2);if(d==0){for(int i=3;i<(b-1);i++){if(a[i]!=0)printf("%d ",a[i]);}printf("%d\n",a[b-1]);}else{for(int i=3;i<n;i++){if(a[i]!=0)printf("%d ",a[i]);}printf("%d\n",a[n]);}printf("%d %d ",c+1,a[2]);for(int i=1;i<c;i++)printf("0 ");printf("0\n");}return 0; } 



0 0