资讯专栏INFORMATION COLUMN

[HackerRank] Plus Minus

leeon / 2865人阅读

Problem

Given an array of integers, calculate which fraction of its elements are positive, which fraction of its elements are negative, and which fraction of its elements are zeroes, respectively. Print the decimal value of each fraction on a new line.

Note

This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to are acceptable.

Input Format

The first line contains an integer, , denoting the size of the array.
The second line contains space-separated integers describing an array of numbers .

Output Format

You must print the following lines:

A decimal representing of the fraction of positive numbers in the array.
A decimal representing of the fraction of negative numbers in the array.
A decimal representing of the fraction of zeroes in the array.
Sample Input

6
-4 3 -9 0 4 1  

   

Sample Output

0.500000
0.333333
0.166667

Solution

import java.io.*;
import java.util.*;
public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int pos = 0, neg = 0, zero = 0;
        int[] A = new int[n];
        for (int i = 0; i < n; i++) {
            A[i] = in.nextInt();
        }
        for (int i = 0; i < n; i++) {
            if (A[i] == 0) zero++;
            else if (A[i] > 0) pos++;
            else neg++;
        }
        System.out.println((float) pos / n);
        System.out.println((float) neg / n);
        System.out.println((float) zero / n);
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66016.html

相关文章

  • MV* 框架 与 DOM操作为主 JS库 的案例对比

    摘要:中定义的处理业务逻辑与提供数据源,中的绑定负责渲染与响应用户点击拖拽等行为,这样就最大保证了视图逻辑相分离。远离的世界,围绕层控制器路由从后端放到前端,更加适合开发。 最近分别使用 Zepto 和 Avalon框架写了个 SPA项目,贴出来讨论下 JS DOM操作为主 JS库 与 MV* 框架的对比 案例(MV* 框架 与 DOM操作 JS库 实例对比) 购物车页面 JS业务逻辑...

    springDevBird 评论0 收藏0
  • 发布一个基于 mprpc_config 二次封装的 pip 包

    摘要:是一个超快速的库,最近一直在看的东西,考虑如何应用到公司的项目中,模仿的方式二次封装了一下。示例示例代码在目录启动和原文发布一个基于二次封装的包博客时空路由器 mprpc 是一个超快速的Python RPC 库,最近一直在看 RPC 的东西,考虑如何应用到公司的项目中,模仿 Celery 的方式二次封装了一下。 项目地址: mprpc_config 安装 pip install mpr...

    sunsmell 评论0 收藏0
  • CSS外挂:Sass 之运算(加、减、乘、除)

    摘要:例如被编译为最后一个栗子字符运算运算符可以用来连接字符串被编译为注意,如果有引号的字符串被添加了一个没有引号的字符串也就是,带引号的字符串在符号左侧,结果会是一个有引号的字符串。 学习Sass无非就是想高效的、 面向对象编写CSS,Sass中的Operations也是重要的一部分。现在的前端各种工程化、模块化、面向工资编程,哦...不对,是面向对象编程。玩起来吧! 1. 加减法 加减法...

    khs1994 评论0 收藏0
  • Python的@装饰器是干什么用的?

    摘要:那么,这个装饰器要怎么定义呢我们来看一下。当然了,如果大家在网络上搜索,关于如何定义装饰器,看到的是一个更加规范的版本。在当中,调用原函数时又,即把输入的元祖解包再传入。 ...

    NusterCache 评论0 收藏0
  • 一道原型上封装的面试题

    摘要:既实现结果为的代码知识点为原型增加方法。主要是考察对包装类是否能用原型属性,包装类上也会有原型方法,只不过是包装类的原型方法用的少而已。类上用的要多别外,类型的值,如果调用原型方法,需要把数学用括号括起来,要不然就是非法的浮点数了。 写一段JS代码,按下面的方式实现plus和minus方法: var a = (7).minus(2).plus(1); console.log(a)//...

    cgh1999520 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<