-1
memoizazion methood를 사용하여 n과 k의 이항 계수 항을 재귀 적으로 계산하는 함수를 작성했습니다. 실행할 때 OutOfBoundException
이 나오면 실수에 대한 몇 가지 지시 사항을 듣게되어 기쁩니다. 끝났어. 감사합니다.memoizazion methood를 사용한 이항 계수
public class Binomial {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int k = Integer.parseInt(args[1]);
StdOut.print(binomial(n, k));
}
/**
* Recursively calculates the binomical coefficient term of n and k using
* the memoizazion methood
*
* @param n
* the upper nonnegative integer of the binomical coefficient
* @param k
* the lower nonnegative integer of the binomical coefficient
* @returns the computed binomical coefficient
*/
public static long binomial(int n, int k) {
long[][] mem = new long[n + 1][k + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
mem[i][j] = -1;
}
}
return binomial(n, k, mem);
}
public static long binomial(int n, int k, long[][] mem) {
if (k > n)
return 0;
if (k == 0 || n == 0)
return 1;
if (mem[n - 1][k] == -1) {
mem[n - 1][k] = binomial(n - 1, k, mem);
}
if (mem[n - 1][k - 1] == -1) {
mem[n - 1][k - 1] = binomial(n - 1, k - 1, mem);
}
return (mem[n - 1][k] + mem[n - 1][k - 1]);
}
}
일부 디버깅을해야합니다. –