BOJ 11050異項係数[Java]



質問する



方法

  • は貴工場で最も基本的に解決します.
  • 注意とこの投稿を書いた理由)を繰り返して解答を試みた結果、間違いの答えが出てきた.その理由はKが0の場合を考慮しないため...気をつけて!
  • インプリメンテーション

    import java.io.*;
    import java.util.*;
    
    class Main {
    
        public static void main(String[] args) throws Exception {
            // for coding
            // System.setIn(new FileInputStream("./input/input_11050.txt"));
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            StringTokenizer st = new StringTokenizer(br.readLine(), " ", false);
    
            int top = Integer.parseInt(st.nextToken());
            int bottom = Integer.parseInt(st.nextToken());
            if (bottom != 0) {
                int cnt = bottom;
                int resTop = top;
                int resBottom = bottom;
    
                // 반복 이항계수
                for (int i = 1; i < cnt; i++) {
                    resTop *= (--top);
                    resBottom *= (--bottom);
                }
                bw.write(resTop / resBottom + "");
            }
            else {
                bw.write("1");
            }
    
            // 재귀 팩토리얼로
            //bw.write(factorial(top) / (factorial(bottom) * factorial(top - bottom)) + "");
    
            bw.close();
        }
    
        // 재귀 팩토리얼로
        // static int factorial(int N) {
        //     if (N == 0) {
        //         return 1;
        //     }
    
        //     return N * factorial(N - 1);
        // }
    }

    送信