suheang

[백준] | JAVA, 자바 | 11050번 - 이항 계수 1 본문

알고리즘

[백준] | JAVA, 자바 | 11050번 - 이항 계수 1

suheang 2024. 3. 29. 20:22

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

 

11050번: 이항 계수 1

첫째 줄에 \(N\)과 \(K\)가 주어진다. (1 ≤ \(N\) ≤ 10, 0 ≤ \(K\) ≤ \(N\))

www.acmicpc.net


문제 요약 :


문제 풀이 :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

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());
        int result = factorial(n) / (factorial(k) * factorial(n - k));

        System.out.println(result);
    }

    public static int factorial(int n) {
        if (n <= 1) return 1;
        return n * factorial(n - 1);
    }
}

 

1. n 과 k 입력받기

2. 위 식을 통해 이항 계수를 계산하고 그 결과를 출력