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 .
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.
Program below:
package com.rohan.test;
import java.util.*;
public class FractionTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
double pcount = 0,ncount = 0, count = 0;
int arr[] = new int[n];
for(int arr_i=0; arr_i < n; arr_i++){
arr[arr_i] = in.nextInt();
}
for(int i = 0; i < n; i++) {
if(arr[i] > 0)
pcount++;
else if(arr[i] < 0)
ncount++;
else count++;
}
//System.out.println(pcount+" "+ncount+" "+count);
System.out.printf("%.6f\n",pcount/n);
System.out.printf("%.6f\n",ncount/n);
System.out.printf("%.6f\n",count/n);
}
}
Sample Input
6
-4 3 -9 0 4 1
Sample Output
0.500000
0.333333
0.166667
Comments
Post a Comment