题解:第一种负数的个数为奇数个,多余的给第二个,第二个数组负数为偶数,第三个有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;
}