• Feed
  • Explore
  • Ranking
/

    [코딩테스트 일지] 해커랭크, Plus Minus

    해커랭크 - 1 Month Preparation Kit 1번 문제.
    B
    Brocolling
    2024.05.26
    ·
    3 min read

    코딩테스트 준비 일지

    • 응시 사이트 : 해커랭크

    • 문제 : Plus Minus

    • 난이도 : 하

    문제

    Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with

    places after the decimal.

    Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to 10^(-4), are acceptable.

    Example

    arr = [1,1,0-1,-1]

    There are n = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000 , 2/5 = 0.400000 , and 1/5 = 0.200000. Results are printed as:

    0.400000
    0.400000
    0.200000

    Function Description

    Complete the plusMinus function in the editor below.

    plusMinus has the following parameter(s):

    • int arr[n]: an array of integers

    Print
    Print the ratios of positive, negative and zero values in the array. Each value should be printed on a separate line with

    digits after the decimal. The function should not return a value.

    Input Format

    The first line contains an integer,

    , the size of the array.
    The second line contains space-separated integers that describe

    Constraints

    • 0 < n <= 100

    • - 100 <= arr[i] <= 100

    Output Format

    Print the following lines, each to decimals:

    1. proportion of positive values

    2. proportion of negative values

    3. proportion of zeros

    Sample Input

    STDIN           Function
    -----           --------
    6               arr[] size n = 6
    -4 3 -9 0 4 1   arr = [-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: 3/6 = 0.500000 , negative: 2/6 = 0.333333 and zeros: 1/6 = 0.166667

    문제 풀이 접근

    뭔가 문제가 영어다보니, 쉬운 문제도 어렵게 보이는 듯한 그런 느낌이 있다.
    하지만, 이 문제는 아래와 같다.
    1. n 개 정수 던져줄게
    2. 양수 / 전체 , 음수 / 전체, 0 / 전체 이렇게 나눠서 값 출력해.

    그래서 던져주는대로, 갯수를 먼저 세고, 양의 정수, 음의 정수, 0 의 값을 비율로 출력했다.
    출력까지 하는줄 몰랐는데, 출력하니 All Case Pass 였다.

    작성 코드 ( C# )

    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Text.RegularExpressions;
    using System.Text;
    using System;
    class Result
    {/*
     * Complete the 'plusMinus' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */
    public static List&lt;double&gt; ratio = new List&lt;double&gt;();public static void plusMinus(List&lt;int&gt; arr)
    {
    double count = arr.Count;
    double posiCount = arr.FindAll(rhs =&gt; rhs &gt; 0).Count;
    double NegaCount = arr.FindAll(rhs =&gt; rhs &lt; 0).Count;
    double ZeroCount = arr.FindAll(rhs =&gt; rhs == 0).Count;ratio.Add(posiCount/count);
    ratio.Add(NegaCount/count);
    ratio.Add(ZeroCount/count);}}class Solution
    {
    public static void Main(string[] args)
    {
    int n = Convert.ToInt32(Console.ReadLine().Trim());    List&lt;int&gt; arr = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp =&gt; Convert.ToInt32(arrTemp)).ToList();Result.plusMinus(arr);
    var ratio = Result.ratio;for(int i = 0; i &amp;lt; ratio.Count; ++i)
    {
    Console.WriteLine(ratio[i]);
    }}}