-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlusMinus.java
44 lines (37 loc) · 1.19 KB
/
PlusMinus.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
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 10^-4 are acceptable.
Link: https://www.hackerrank.com/challenges/plus-minus
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int count = s.nextInt();
printPlusNeg(s, count);
}
public static void printPlusNegZero(Scanner s, int count) {
int neg = 0;
int pos = 0;
int zero = 0;
for (int i = 0; i < count; i++) {
int num = s.nextInt();
if (num == 0)
zero++;
else if (num > 0)
pos++;
else if (num < 0)
neg++;
}
System.out.println((double)pos/count);
System.out.println((double)neg/count);
System.out.println((double)zero/count);
}
}