미소를뿌리는감자의 코딩

[백준 2024/01/19] 11050번 이항 계수 1 본문

코딩 테스트/백준

[백준 2024/01/19] 11050번 이항 계수 1

미뿌감 2024. 1. 19. 19:57
728x90

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

 

11050번: 이항 계수 1

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

www.acmicpc.net

 

1. 접근 방법

 

이번 문제는 가뭄의 단비 같은 느낌 이었다~ㅎㅎ

오랜만에 빠르게 해결할 수 있던 문제였다.

 

+ 함수를 만들어서 사용하는 것이 요즘 예쁘고 매력적이게 느껴진다.

앞으로 코드를 작성할 때, 함수를 잘 사용해 보고 싶다.

 

2. 코드

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Deque;
import java.util.ArrayDeque;


public class b11050 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String line = reader.readLine();
        String[] num = line.split("\\s+");
        int n = Integer.parseInt(num[0]);
        int r = Integer.parseInt(num[1]);

        System.out.println(combination(n,r));

    }
    public static int combination(int n, int r){
        return factorial(n)/(factorial(r)*factorial(n-r));
    }

    public static int factorial(int num){
        int f_result = 1;
        for(int i = num; i >0;i--){
            f_result*= i;
        }
        return f_result;
    }

}
728x90