统计组合数的个数

例如: 从5个不同的球(A B C D E)中取3个共有10中情况

如下图:

A B C

A B D

A B E

A C D

A C E

A D E

B C D

B C E

B DE

C D E

下假设将C球图上颜色,则上面的10中情况,可细分为包含涂色的球(3个球中一个已确定,剩余两球在四种求中选择)和不包含涂色的球(3个球中都不确定,但范围缩小为4个球)

包含C的:                                               不包含C的

A B C                                                  A B D

A C D                                                  A B E

A C E                                                  A D E

B C D                                                  B D E

B C E

C D E

数学证明:

代码

    // 获取组合数量
    public static int getCombinationCount(int n, int m) {    
        if (n == m) // n个中选m个,只有1种
            return 1;
        if (m == 1)  // n个中选1个,n中选法
            return n;
        return getCombinationCount(n-1, m) + getCombinationCount(n-1, m-1);
    }
原文地址:https://www.cnblogs.com/hupeng1234/p/6832481.html