728x90
SMALL

using System;

public class Solution
{
    public double solution(int balls, int share)
    {
        double answer = 0;
        double sum1 = 1;
        double sum2 = 1;
        double sum3 = 1;
        
        for (double i = 1; i <= balls; i++)
        {
            sum1 *= i;
        }
        
        for (double i = 1; i <= share; i++)
        {
            sum2 *= i;
        }
        
        for (double i = 1; i <= balls - share; i++)
        {
            sum3 *= i;
        }
        answer = Math.Round(sum1 / (sum2 * sum3));

        return answer;
    }
}

 

////

 

using System;
using System.Numerics;

public class Solution 
{
    public int solution(int balls, int share)
    {
        return Combination(balls, share);
    }

    public int Combination(int n, int m)
    {
        if (m == 0 || n == m) 
        {
            return 1;
        }

        return Combination(n - 1, m - 1) + Combination(n - 1, m);
    }
}

 

 

//////////

파이썬

 

import math

def solution(balls, share):
    return math.comb(balls, share)

 

def solution(balls, share):
    answer = factorial(balls) / (factorial(balls - share) * factorial(share))
    return answer

def factorial(n):
    result = 1
    for i in range(1, n + 1):
        result = result * i
    return result

 

자바

//////

 

class Solution {
    public long solution(int balls, int share) {
        long answer = 0;

        int d = (balls - share) > share ? share : balls - share;
        if (d == 0) return 1;

        return solution(balls - 1, d - 1) * balls / d;
    }
}

 

 

import java.math.BigInteger;

class Solution {
    public BigInteger solution(int balls, int share) {
        return factorial(balls).divide(factorial(balls - share).multiply(factorial(share)));
    }

    public BigInteger factorial(int n) {
        BigInteger result = new BigInteger("1");
        BigInteger from = new BigInteger("1");
        BigInteger to = new BigInteger(String.valueOf(n));

        for (BigInteger i = from; i.compareTo(to) <= 0; i = i.add(BigInteger.ONE)) {
            result = result.multiply(i);
        }
        return result;
    }
}

 

 

class Solution {
    public long solution(int balls, int share) {
        share = Math.min(balls - share, share);

        if (share == 0)
            return 1;

        long result = solution(balls - 1, share - 1);
        result *= balls;
        result /= share;

        return result;
    }
}
728x90

설정

트랙백

댓글