반응형

코딩놀이! 10 Days of Statistics(HackerRank)

 Day 4-1: Binomial Distribution I



9 번째 문제!

 Objective 

In this challenge, we learn about binomial distributions. Check out the Tutorial tab for learning materials!

Task 
The ratio of boys to girls for babies born in Russia is . If there is  child born per birth, what proportion of Russian families with exactly  children will have at least  boys?

Write a program to compute the answer using the above parameters. Then print your result, rounded to a scale of  decimal places (i.e.,  format).

Input Format

A single line containing the following values:

1.09 1

If you do not wish to read this information from stdin, you can hard-code it into your program.

Output Format

Print a single line denoting the answer, rounded to a scale of  decimal places (i.e.,  format).


이번 문제는 간단한 남녀 출생 비율! 구하기 입니다.


그럼 바로 문제 해석 들어갑니닷!


문제해석!

1. 러시아 에서 태어난 아기는 남학생:여학생 (1.09:1) 비율이다

2. 6명의 아이를 가진 러시아 가정 중에 3명의 아들을 가질 수 있는 비율은?

3. 소수점 셋째 자리로 나타내라!(반올림)

계산식!!!

1. 남자, 여자가 태어 낫을때 남자가 태어날 비율을 구한다!(남자비율/남자+여자비율)

2. 여자가 태어날 비율도 1번과 똑같이 구한다(여자비율/남자+여자비율)or(1-1번비율)

3. 아기가 태어났을때 남자일 확률+아기가태어낫을때 남자일확률+아기가태어낫을때 남자일 확률

4. 끝이네요 ㅎㅎ


역시 말보단 코딩!

  class Solution

    {

        public static readonly double BOYS_RATIO = 1.09;

        public static readonly double GIRLS_RATIO = 1;

        public static readonly int BOYS = 3;

        public static readonly int BABY = 6;

        static void Main(String[] args)

        {

            Console.WriteLine(binomialDistribution());

        }

        static string binomialDistribution()

        {

            double p = (BOYS_RATIO) / (BOYS_RATIO + GIRLS_RATIO);

            double q = 1 - p;


            double probability = 0.0;

            for (int i = BABY; i >= BOYS; i--)

            {

                probability += combination(i) * Math.Pow(p, i) * Math.Pow(q, 6 - i);

            }

            return String.Format("{0:0.000}", probability);

        }

        static double combination(int r)

        {

            return (factorial(BABY) / (factorial(r) * factorial(BABY - r)));

        }

        static long factorial(int n)

        {

            long resultFactorial= 1;

            for (int i = 1; i <= n; i++)

            {

                resultFactorial*= i;

            }

            return resultFactorial; ;

        }

    }


끝입니다!ㅎㅎ

사실 오랫만에 계산하려니 머리가 아프더군요 ㅎㅎ

공책펴놓고 두장정도 쓴거같네요 ㅎㅎ(하..드..코코딩?, 하드계산식?) ㅋㅋ

이 문제의 핵심은 6명의 아기중 3명의 남자가 태어날 확률이예요.
포인트는!. (남자태어날확률/6명의아기확률+5명의아기확률)+...+(남자태어날확률/4명의아기확률+3명의아기확률)


역시나 이번에도 Math.Pow가 등장했네요.

public static void Main() { int value = 2; for (int power = 0; power <= 32; power++) Console.WriteLine("{0}^{1} = {2:N0} (0x{2:X})", value, power, (long)Math.Pow(value, power)); }

MS의 Framework 4.7 Document에 자세한 설명!(출처:https://docs.microsoft.com/en-us/dotnet/api/system.math.pow?view=netframework-4.7.2)


간단 구현!

class Math

{

public static double pow(double value, double power)

{

double result;

for(int index=0; index<=power power++)

{

result *=value;

}

return result;

}

}

네요 ㅎㅎ

전....이런거 만드는게 더 재미있는거같아요 ㅠㅠ

하지만, Framework 에서 지원하는 기능은 그냥 쓰는걸로...

물론!. Framework에서 지원하는 함수를 쓰다가 가~~~~~~~~~~~~~~~끔 잘못된 값이 넘어오면 찾기도 힘들고...눈도빠지고.... 후 디버그 자주자주 습관화 및! 값 검증!!assert 제대로 쓰기!!!



RunCode!!

Input (stdin)Download
1.09 1
Expected OutputDownload
0.696
Compiler Message
Success


Day 4에는 4문제가 있네요..

죽을 사 인가



그럼이만! 혹시 설명이 부족하다면 댓글 달아주세용 ㅎ


(출처:https://www.hackerrank.com/challenges/s10-binomial-distribution-1/problem)

반응형

+ Recent posts