我正在寻找一种更好地计算以下内容的方法。 问题是我无法弄清楚这种组合的名称。 首先我想如果我有 3 个“a”和 2 个“b”, 那么我可以每次将b放在a+1位置_a_a_a_,持续(a+1)^b次,但是位置0然后1或1然后0的选择顺序是相同的:即“baba”。
我已经找到了这种递归方法来计算它。
/* find all permutations of a non unique sequence of 0s and 1s
... "a"s and "b"s etc... */
/* e.g. how many ways to arange 4 apples and 3 oranges, 35 */
int *perms(int a, int b){
static int total = 0;
if(!a && !b){
total++;
return 0;
}
if(a > 0){
perms(a-1,b);
}
if(b > 0){
perms(a,b-1);
}
return &total;
}
我有一种强烈的感觉,我可以做得更好。
最佳答案
拉斯曼斯在评论中给出的答案是(a+b)!/(a!* b!) 或
factorial(a + b) / (factorial(a) * factorial(b))
关于c - 计算多重集的唯一排序数的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23247615/