ausleaf / daily-algorithm

0 stars 0 forks source link

Plus Minus #4

Open ausleaf opened 5 years ago

ausleaf commented 5 years ago

문제) https://www.hackerrank.com/challenges/plus-minus/problem

양의정수, 음의정수, 0으로 이루어진 숫자배열에서 양의정수 비율, 음의정수 비율, 0의 비율의 순서대로 출력하는 문제

풀이) https://github.com/Seoyeong-Kim/daily-algorithm/tree/master/src/main/java/hackerrank/plusminus

ausleaf commented 5 years ago
        int positiveCount = 0;
        int zeroCount = 0;
        int negativeCount = 0;

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 0) {
                positiveCount++;
            } else if (arr[i] < 0) {
                negativeCount++;
            } else {
                zeroCount++;
            }
        }

        System.out.println((double) positiveCount / arr.length);
        System.out.println((double) negativeCount / arr.length);
        System.out.println((double) zeroCount / arr.length);