본문 바로가기
Algorithm

백준 11050번 이항계수 1

by 2nyong 2023. 3. 10.

주어진 두 숫자(N, K)의 이항계수를 구하는 문제다.

 

이항계수 공식

본 문에서는 공식의 r 대신 K를 대입한다.

 

이항계수 공식에 따라 fatorial을 재귀함수로 구현하여 풀이했다.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int N = in.nextInt();
        int K = in.nextInt();

        int ans = factorial(N) / (factorial(K) * factorial(N - K));

        System.out.println(ans);
    }

    public static Integer factorial(Integer n) {

        if (n <= 1) {
            return 1;
        }

        return n * factorial(n - 1);
    }
}

'Algorithm' 카테고리의 다른 글

백준 11399번 ATM  (0) 2023.03.11
백준 1541번 잃어버린 괄호  (0) 2023.03.10
백준 11047번 동전 0  (0) 2023.03.10
백준 1037번 약수  (2) 2023.03.09
백준 1927번 최소 힙  (0) 2023.03.09

댓글