点击打开题目
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:
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一起输出;如果没有正数,输出两个负数,其他的和0输出。
注意格式。
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int num[111];
int n;
while (~scanf ("%d",&n))
{
for (int i = 1 ; i <= n ; i++)
scanf ("%d",&num[i]);
sort (num+1,num+n+1);
printf ("1 %d\n",num[1]);
if (num[n] > 0) //有正数的情况,输出一个正数即可
{
printf ("1 %d\n",num[n]);
printf ("%d",n-2);
for (int i = 2 ; i < n ; i++)
printf (" %d",num[i]);
printf ("\n");
}
else //没有正数输出两个负数
{
printf ("2 %d %d\n",num[2],num[3]);
printf ("%d",n-3);
for (int i = 4 ; i <= n ; i++)
printf (" %d",num[i]);
printf ("\n");
}
}
return 0;
}