组合数求解(两种方法)

1)

long C(int n, int m) {
if (n - m < m)
m = n - m;
long ans = 1;
for (int i = 1; i <= m; i++)
ans = ans * (n - i + 1) / i;
return ans;
}

 

2)

static final int MAXN = 30; // max size

void init() {
int C[][] = new int[MAXN][MAXN];
C[0][0] = 1;
for (int i = 1; i < MAXN; i++) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++) {
C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
}
}
}

 

 

posted on 2013-02-25 22:42  Sure_Yi  阅读(316)  评论(1编辑  收藏  举报

导航