글
프로그래머스 C# 구슬을 나누는 경우의 수(Combination, using System.Numerics;, P, J)
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;
}
}
'프로그래밍' 카테고리의 다른 글
프로그래머스 C# 다음 큰 숫자(이진수 변환, 이진법, Convert.ToString, Convert.ToInt32, JAVA) (0) | 2023.01.22 |
---|---|
프로그래머스 C# 컨트롤 제트(인덱스 설정하기, P, J) (0) | 2023.01.22 |
프로그래머스 C# 나누어 떨어지는 숫자 배열(List sort하기, list.add, List<int>, JAVA) (0) | 2023.01.21 |
프로그래머스 C# 2016년(int형 배열 생성, Datetime, JAVA) (0) | 2023.01.21 |
프로그래머스 C# 두 정수 사이의 합(삼항연산자, JAVA) (0) | 2023.01.21 |