본문 바로가기
1 Day 1 Algorithms

[2018.12.17] Plus Minus

by 곰돌찌 2018. 12. 17.

Problem


Given an array of integers, calculate the fractions of its elements that are positivenegative, and are zeros. 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.

For example, given the array  there are  elements, two positive, two negative and one zero. Their ratios would be  and . It should be printed as

0.400000
0.400000
0.200000

Function Description

Complete the plusMinus function in the editor below. It should print out the ratio of positive, negative and zero items in the array, each on a separate line rounded to six decimals.

plusMinus has the following parameter(s):

  • arr: an array of integers

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 .

Constraints

 

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 zeros in the array compared to its size.

Sample Input

6
-4 3 -9 0 4 1         

Sample Output

0.500000
0.333333
0.166667

Explanation

There are  positive numbers,  negative numbers, and  zero in the array. 
The proportions of occurrence are positive: , negative:  and zeros: .


How I solved the problem


# 음수, 양수, 0 이 각각 몇개 있는지 판단하여 해당 배열에서 양수, 음수, 0의 각각 비율이 어느정도인지 계산하는 문제


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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) {
        int count = arr.length;
        double positive = 0.0;
        double negative = 0.0;
        double zeros = 0.0;
        
        for (int i = 0; i < count; i++) {
            if (arr[i] > 0) {
                positive ++;
            } else if (arr[i] < 0) {
                negative ++;
            } else {
                zeros ++;
            }
        }
 
        System.out.println(Math.round((positive / (double) count) * 1000000/ 1000000.0);
        System.out.println(Math.round((negative / (double) count) * 1000000/ 1000000.0);
        System.out.println(Math.round((zeros / (double) count) * 1000000/ 1000000.0);
    }
 
    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();
    }
}
 
cs


[출처 : https://www.hackerrank.com ]

'1 Day 1 Algorithms' 카테고리의 다른 글

[2018.12.18] Staircase  (0) 2018.12.18
[2018.12.18] Arrays  (0) 2018.12.18
[2018.12.17] Let's Review  (0) 2018.12.17
[2018.12.16] Loops  (0) 2018.12.17
[2018.12.15] Class vs. Instance  (0) 2018.12.17

댓글