zoukankan      html  css  js  c++  java
  • codeforces300A 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 n(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 n(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 n(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<stdio.h>
    #include<string.h>
    #include<cstdio> 
    #include<string>
    #include<math.h>
    #include<algorithm>
    #define LL long long
    #define PI atan(1.0)*4
    #define DD double
    #define MAX 300
    #define mod 100
    #define dian 1.000000011
    #define INF 0x3f3f3f
    using namespace std;
    int s[MAX],k[MAX];
    int main()
    {
    	int n,m,j,i,t,a,b,c;
    	while(scanf("%d",&n)!=EOF)
    	{
    		for(i=0;i<n;i++)
    		    scanf("%d",&s[i]);
    		int flag=0;
    		for(i=0;i<n;i++)
    		{
    			if(s[i]>0)
    			{
    				b=i;
    				flag=1;
    			}
    		}
    		for(i=0;i<n;i++)
    	    {
    	    	if(s[i]<0)
    	    	{
    	    		c=i;
    	    		break;
    	    	}
    	    }
    	    j=0;
    	    if(!flag)
    	    for(i=0;i<n;i++)
    	    {
    	    	if(i!=c)
    	    	{
    	    		if(s[i]<0)
    	    		    k[j++]=s[i];
    	    	}
    	    }
    	    printf("1 %d
    ",s[c]);
    	    if(flag)
    	    	printf("1 %d
    ",s[b]);
    	    else
    	    	printf("2 %d %d
    ",k[0],k[1]);
    	    if(flag)
    	    {
    	    	printf("%d ",n-2);
    	    	for(i=0;i<n;i++)
    	    	{
    	    		if(s[i]!=s[c]&&s[i]!=s[b])
    	    		printf("%d ",s[i]);
    	    	}
    	    	printf("
    ");
    	    }
    	    else
    	    {
    	    	printf("%d ",n-3);
    	    	for(i=0;i<n;i++)
    	    	{
    	    		if(s[i]!=s[c]&&s[i]!=k[0]&&s[i]!=k[1])
    	    		printf("%d ",s[i]);
    	    	}
    	    	printf("
    ");
    	    }
    	}
    	return 0;
    } 
    

      

  • 相关阅读:
    Linux系统下设置Tomcat开机自动启动
    2、nginx配置文件基本介绍
    1、Centos7下安装nginx
    40、springboot——运行状态监控使用Actuator
    39、springboot——热部署
    38、springboot——分布式之SpringCloud
    38、springboot——分布式之zookeeper+dubbo
    37、springboot——安全
    36、springboot——异步任务、定时任务、邮件任务
    React过誉了吗?
  • 原文地址:https://www.cnblogs.com/tonghao/p/5316503.html
Copyright © 2011-2022 走看看