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 compared to its size.
- A decimal representing of the fraction of negative numbers in the array compared to its size.
- A decimal representing of the fraction of zeroes in the array compared to its size.
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int arr[] = new int[n]; for(int arr_i=0; arr_i < n; arr_i++){ arr[arr_i] = in.nextInt(); } int negative = 0; int positive = 0; int zeros = 0; for (int i = 0; i < n; i++) { if (arr[i] > 0) { positive++; } else if (arr[i] < 0) { negative++; } else { zeros++; } } System.out.println((double) positive / n); System.out.println((double) negative / n); System.out.println((double) zeros / n); } }
'Programming > >> Algorithm' 카테고리의 다른 글
[Lucky Algorithm] Birthday Cake Candles (0) | 2017.10.03 |
---|---|
[Lucky Algorithm] Staircase (0) | 2017.10.03 |
[Lucky Algorithm] Diagonal Difference (0) | 2017.10.03 |
[Lucky Algorithm] A Very Big Sum (0) | 2017.10.03 |
[Lucky Algorithm] Compare the Triplets (0) | 2017.10.03 |
댓글