回覆列表
-
1 # 小紅的甜心
-
2 # 王憶豪
為啥非要遞迴呢。。。
int answer(int n, int k) {
if (k > n) {
puts(“Error\n”);
return 0;
}
while (k > 1) {
n*=n-1;
n- -;
k*=k-1;
k- -;
}
printf(“%d\n”, n / k);
return n / k;
}
//遞迴的話需要用到組合的遞推公式: C(n, k) = C(n - 1, k - 1) + C(n - 1, k)
int tar_func(int ** res, int n, int k){
if( k > n) return -1;
if(k == n) return 1;
int res1 = 0, res2 = 0;
if(res[n - 1] [ k-1] == 0){
res1 = tar_func(res, n-1, k-1);
if(res1 == -1) return -1;
else{
res[n-1] [k-1] = res1;
}
}else{
res1 = res[n-1][k-1];
}
if(res[n-1][k] == 0){
res2 = tar_func(res, n-1, k);
if(res2 == -1) return -1;
else res[n-1][k] = res2;
}else{
res2 = res[n-1][k];
}
res[n][k] = res1 + res2;
return res[n][k];
}