Algorithm/Baekjoon Online Judge

[Java] [Math] 백준 11050 이항 계수 브론즈1

제우제우 2024. 5. 4. 16:57

https://www.acmicpc.net/problem/11050

import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());

        // 이항 계수 -> 순열 n!/k! * (n-k)! 5!/2! * 3! = 10

        if(k == 0){
            System.out.println(1);
            return;
        }
        int cnt = 1;
        for (int i = n - k + 1; i <= n; i++) {
            cnt *= i;
        }
        for (int i = 1; i <= k; i++) {
            cnt /= i;
        }
        System.out.println(cnt);

    }
}