본문 바로가기
Algorithm/Baekjoon

Baekjoon 2407 조합 JAVA

by Hunveloper 2022. 4. 19.
728x90
 

2407번: 조합

n과 m이 주어진다. (5 ≤ n ≤ 100, 5 ≤ m ≤ 100, m ≤ n)

www.acmicpc.net

문제

nCm을 출력한다.

입력

n과 m이 주어진다. (5 ≤ n ≤ 100, 5 ≤ m ≤ 100, m ≤ n)

출력

nCm을 출력한다.

풀이

BigInteger를 이용하여 값을 계산하여 출력

코드
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception{
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt(), m=sc.nextInt();
		BigInteger up = new BigInteger("1"), down = new BigInteger("1");
		if(n-m<m)
			m=n-m;
		for(int i=0;i<m;i++) {
			up=up.multiply(new BigInteger(String.valueOf(n-i)));
			down=down.multiply(new BigInteger(String.valueOf(i+1)));
		}
		System.out.println(up.divide(down));
	}
}

 

728x90
728x90

댓글