반응형

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

 Day 4-2: Binomial Distribution II



10 번째 문제!

 Objective 

In this challenge, we go further with binomial distributions. We recommend reviewing the previous challenge's Tutorial before attempting this problem.

Task 
A manufacturer of metal pistons finds that, on average,  of the pistons they manufacture are rejected because they are incorrectly sized. What is the probability that a batch of  pistons will contain:

  1. No more than  rejects?
  2. At least  rejects?

Input Format

A single line containing the following values (denoting the respective percentage of defective pistons and the size of the current batch of pistons):

12 10

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

Output Format

Print the answer to each question on its own line:

  1. The first line should contain the probability that a batch of  pistons will contain no more than  rejects.
  2. The second line should contain the probability that a batch of  pistons will contain at least  rejects.

Round both of your answers to a scale of  decimal places (i.e.,  format).


이번 문제는  Binomial Distribution I 과 비슷한 문제입니다.

공장에서 만든 불량품! 확률 구하기?정도로 해석되네요 ㅎㅎ


문제해석!

1. 피스톤 공장에서 평균 12%정도 불량이 발생한다!

2. 10개의 피스톤을 만들었을때 다음의 확률을 구해라!

3. 장애 피스톤 갯수 2개 이하 발생 확률

4. 장애 피스톤 최소 2개 이상 발생 확률

5. 둘다 구해서 소수점 셋째 자리로 나타내라!(반올림), 그리고 출력해라

계산식 포인트

1. n!/(n-1)! *p(pow)x * q(pow)(n-x)

  *n=10개피스톤 , p=불량 확률(12%->12/100->0.12), q=정상 확률(1-불량확률), x=2개 불량 피스톤 or 2개이상 불량 피스톤)*

2. 이전 문제(남녀비율)과 마찬가지로 장애확률/정상확률+장애확률 을 구하면되는게 포인트입니다.

3. 2번의 확률을 구하면 2개 이하 발생확률과 2개 이상 발생확률은 반복문으로 돌려서 더하면 원하는 합산확률이 나와요.


*이번문제는 꼭 한번 스스로 풀어보세요. 강츄강츄 저는 아직도 공책에 논리식을 써보는게 더 편하네요 ㅎㅎ*


역시 말보단 코딩!

class Solution
    {
        public static readonly double BAD_PROBABILITY = 0.12;
        public static readonly int BATCH_PISTONS = 10;
        public static readonly int BAD_PISTONS = 2;
        static void Main(String[] args)
        {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        
            foreach(string temp in binomialDistribution2())
            {
                Console.WriteLine(temp);
            }
        }

        static string[] binomialDistribution2()
        {
            double NoMoreProbability = 0.0;
            double AtLeastProbability=0.0;
            for (int i = 0; i <= BAD_PISTONS; i++)
            {
                NoMoreProbability += combination(i) * Math.Pow(BAD_PROBABILITY, i) * Math.Pow(1 - BAD_PROBABILITY, BATCH_PISTONS - i);
            }
            
            for (int i = BATCH_PISTONS; i >= BAD_PISTONS; i--)
            {
                AtLeastProbability += combination(i) * Math.Pow(BAD_PROBABILITY, i) * Math.Pow(1 - BAD_PROBABILITY, BATCH_PISTONS - i);
            }
            
            //string result = String.Format("{0:0.000}", NoMoreProbability) +" "+ String.Format("{0:0.000}", AtLeastProbability);
            string[] result = { String.Format("{0:0.000}", NoMoreProbability), String.Format("{0:0.000}", AtLeastProbability) };
            return result;
        }

        static double combination(int i)
        {
            return (factorial(BATCH_PISTONS) / (factorial(i) * factorial(BATCH_PISTONS - i)));
        }

        static long factorial(int n)
        {
            long tempResult = 1;
            for (int i = 1; i <= n; i++)
            {
                tempResult *= i;
            }
            return tempResult; ;
        }
    }


후기!. 역시 난 영어를 못한다. papago만세.



제출!!!


30점 획득!


이번문제는 테스트 케이스 볼라면 5 hackos 바치라고해서 안봣습니다.ㅠ

열심히 hackos모아야지 레벨업업업업....귀찮 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ


4일차 4문제중 2개 Clear!




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


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

반응형

+ Recent posts