【codeforces】A. Array(思维水题)


点击打开题目


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 -1
1 2
1 0
input
4
-1 -2 -3 0
output
1 -1
2 -3 -2
1 0

题意:把输入的数字分割成三种数组,第一种元素之积为负,第二种为正,第三种为0‘

题解:第一种负数的个数为奇数个,多余的给第二个,第二个数组负数为偶数,第三个有0就行。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
	int n;
	while(cin>>n){
		int a1[110],a2[110],a3[110];
		int k1=0,k2=0,k3=0;
		int temp;
		memset(a1,0,sizeof(a1)); 
		memset(a2,0,sizeof(a2));
		memset(a3,0,sizeof(a3));
		for(int i=0;i<n;i++){
			cin>>temp;
			if(temp<0)
			a1[k1++]=temp;
			else if(temp==0)
			a2[k2++]=temp;
			else
			a3[k3++]=temp;
		}
		int flag1,flag2,flag3;
		flag1=flag2=flag3=0;
		int m1=k1;
		int m2=k2;
		int m3=k3;
		if(!m3){//正数是否为空
			a3[0]=a1[m1-1];
			a3[1]=a1[m1-2];
			m1-=2;
			m3+=2;
		}
		if(m1&1){//判断是否为奇数个负数
			cout<<m1<<" ";
			for(int i=0;i<m1-1;i++)
			cout<<a1[i]<<" ";cout<<a1[m1-1]<<endl;
		}
		else{//否则的话,减掉一个
			a2[m2]=a1[m1-1];
			m2++;
			m1-=1;
			cout<<m1<<" ";
			for(int i=0;i<m1-1;i++)
			cout<<a1[i]<<" ";cout<<a1[m1-1]<<endl;
		}
			cout<<m3<<" ";
			for(int i=0;i<m3-1;i++)
			cout<<a3[i]<<" ";cout<<a3[m3-1]<<endl;
			cout<<m2<<" ";
			for(int i=0;i<m2-1;i++)
			cout<<a2[i]<<" ";cout<<a2[m2-1]<<endl;
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值