알고리즘/수학

[백준 11050] - [수학] - 이항계수 1 (Java)

팡스 2018. 12. 14. 17:12

문제 링크 : https://www.acmicpc.net/problem/11050




이문제는 간단한 조합 문제이다. 


입력받는 형식을 식으로 풀이하면 아래와 같이 나온다.




이 문제는 제일 간단한 문제이기때문에 그냥 각 수 별로 {n!, k!, (n-k)! }  팩토리얼 을 구한다음 계산하면 된다.



소스


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int N = sc.nextInt();
        int K = sc.nextInt();
        
        int a = getFactorial(N);
        int b = getFactorial(K);
        int c = getFactorial(N-K);
        
        int result = a/(b*c);
        System.out.println(result);
    }
    
    public static int getFactorial(int n) {
        int result = 1;
        while(n > 1) {
            result *= n--;
        }
        return result;
    }
}
cs