728x90
문제
nCm을 출력한다.
입력
n과 m이 주어진다. (5 ≤ n ≤ 100, 5 ≤ m ≤ 100, m ≤ n)
출력
nCm을 출력한다.
굳이 재귀로 팩토리얼을 구하지 않아도 간단하게 풀 수 있지만, 수의 범위가 커서 BigInteger 클래스를 사용해야 한다.
package BOJ0809;
import java.math.BigInteger;
import java.util.Scanner;
public class BOJ_2407_조합 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger n = new BigInteger(Integer.toString(sc.nextInt()));
BigInteger m = new BigInteger(Integer.toString(sc.nextInt()));
BigInteger cnt = new BigInteger("0");
BigInteger result = new BigInteger("1");
while (cnt.compareTo(m) < 0) {
result = result.multiply(n);
cnt = cnt.add(BigInteger.ONE);
n = n.subtract(BigInteger.ONE);
}
while (m.compareTo(BigInteger.ZERO) > 0) {
result = result.divide(m);
m = m.subtract(BigInteger.ONE);
}
System.out.println(result.toString());
}
}
728x90
'문제 풀이 > BOJ' 카테고리의 다른 글
[백준] 2527 - 직사각형 (자바/Java) (0) | 2022.08.11 |
---|---|
[백준] 2567 - 색종이 2 (자바/Java) (0) | 2022.08.10 |
[백준] 2564 - 경비원 (자바/Java) (0) | 2022.08.09 |
[백준] 1244 - 스위치 켜고 끄기 (자바/Java) (0) | 2022.08.09 |
[백준] 3273 - 두 수의 합 (자바/Java) (0) | 2022.08.07 |