zoukankan      html  css  js  c++  java
  • codeforces A. Array 解题报告

    题目链接:http://codeforces.com/problemset/problem/300/A

    题目意思:给出n个数,将它们分成三批:1、所有数相乘的结果 < 0    2、所有数相乘的结果 > 0;   3、所有数相乘的结果 = 0   还需要满足一个条件:n个数的归属只可以是其中的一批。

          由于翻译的时候总是以整个短语来翻,因此一直误以为“product”是“产物”的意思,多谢乌冬兄指点迷津。

          不难想到对所有数进行排序,最小的那个数绝对是负数,因此第一批数放1个即可;最大的那个数归到第2批(也是1个),但有个问题,有可能最大的那个数是0,此时第2批要放2两个数(负数),保证相乘是整数,其余放在第3批即可(0和任意一个数相乘都为0),稍稍注意就可以过。

        

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    const int maxn = 1000 + 10;
    int a[maxn];
    
    int main()
    {
        int n, i, j;
        while (scanf("%d", &n) != EOF)
        {
            for (i = 0; i < n; i++)
                scanf("%d", &a[i]);
            sort(a, a+n);
            printf("1 %d
    ", a[0]);
            if (n == 3)
                printf("1 %d
    1 %d
    ", a[2], a[1]);
            else
            {
                if (a[n-1])
                {
                    printf("1 %d
    ", a[n-1]);
                    printf("%d ", n-2);
                }
                else
                    printf("2 %d %d
    %d ", a[1], a[2], n-3);
                i = (a[n-1] ? 1 : 3);   // 记录第2批数的起始
                j = (a[n-1] ? n-1 : n);  // 记录第2批数的终点         
                for ( ; i < j; i++)
                    printf("%d ", a[i]);
                printf("
    ");
            }
        }
        return 0;
    }

        

  • 相关阅读:
    mysql基础操作
    网页粒子背景
    将Myeclipse项目改成Eclipse项目
    mybatis入门配置和调试
    《增删改查返回问题》
    AES加密与解密(秘钥)
    svn下载代码cleanup失败解决办法
    maven中net.sf.json报错
    idea提交SVN时忽略某些文件或文件夹
    《面试常问到的知识点》
  • 原文地址:https://www.cnblogs.com/windysai/p/3537514.html
Copyright © 2011-2022 走看看