본문 바로가기

Algorithm

Plus Minus

반응형

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the plusMinus function below.
    static void plusMinus(int[] arr) {
        double positiveCount = 0;
        double negativeCount = 0;
        double zeroCount = 0;
        int length = arr.length;
        
        for(int num : arr) {
            if(num < 0) negativeCount++;
            else if(num > 0) positiveCount++;
            else zeroCount++;
        }
      

        System.out.printf("%.6f\n", positiveCount / length);  
        System.out.printf("%.6f\n",  negativeCount / length); 
        System.out.printf("%.6f\n",  zeroCount /  length); 

    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        int[] arr = new int[n];

        String[] arrItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int arrItem = Integer.parseInt(arrItems[i]);
            arr[i] = arrItem;
        }

        plusMinus(arr);

        scanner.close();
    }
}

 

 

Intger Array 를 입력받아 양수, 음수, 0의 갯수를 카운팅하여 전체 길이로 나눈 값을 소숫점 6자리 까지 표현하는 문제

 

문제 출처 : www.hackkerrank.com

반응형

'Algorithm' 카테고리의 다른 글

Mini-Max Sum  (0) 2020.10.10
Staricase  (0) 2020.10.10
A Very Big Sum  (0) 2020.10.10
Compare the Trrplets  (0) 2020.10.10
Simple Array Sum  (0) 2020.10.10