본문 바로가기
Programming/>> Algorithm

[Lucky Algorithm] Plus Minus

by 니키ᕕ( ᐛ )ᕗ 2017. 10. 3.

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:

  1. A decimal representing of the fraction of positive numbers in the array compared to its size.
  2. A decimal representing of the fraction of negative numbers in the array compared to its size.
  3. 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);
    }
}


댓글