[HackerRank] Plus Minus

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.5000000.3333330.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  0) pos++;            else neg++;        }        System.out.println((float) pos / n);        System.out.println((float) neg / n);        System.out.println((float) zero / n);    }}

关键字:java, fraction, the, int


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部