组合数个数的代码实现

组合数公式

在这里插入图片描述

代码实现

明确数据类型

首先要先明确运算的数据类型。排列组合数是一组数字总共有多少种排列组合,所以运算的结果一定是整数。
记C(m, n)为从n个数中选取m个数,这些数无序有多少种不同的选法
记A(m, n)为从n个数中选取m个数,且这些数是有序的有多少种不同的选法

求解方法1:先求出分子分母如何做除法

public class Main {
    public static int arrayNumber(int m, int n){
        if (m == 0) return 1;
        int sum = 1;
        while (m > 0){
            sum *= n;
            n--;
            m--;
        }
        return sum;
    }
    public static int combinationNumber(int m, int n){
        return arrayNumber(m,n)/arrayNumber(m,m);
    }
    public static void main(String args[]) {
        System.out.println(arrayNumber(2,4));
        System.out.println(combinationNumber(2,4));
    }
}

这种方法先求出排列数,而排列数是n!, 16! = 2004189184,就已经快超过int最大范围。所以,这种先求出排列数再求出组合数的方法值针对数量比较小的。

求法2

在这里插入图片描述
看到在各个展开式,我们可以把它拆开,变成(n/m) * (n-1)/(m-1) *…
这样就会出现一个问题,那就是(n/m)不一定会是整数,解决办法是继续再组合(n * (n-1))/m,如果还除不了就再乘上一个(n-2).
这样代码实现有一定困难,那么就把分母从n到(n-m+1),而分母从0到m,这样就能保证分子/分母一定是整数。

  • 代码实现
    public static int combinationNumber3(int m, int n){
        int cnt = 1;
        for(int i = 0; i < m; i++){
            cnt *= n - i;
            cnt /= i + 1;
        }
        return cnt;
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是Java中实现排列组合的代码示例: ``` import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class PermutationCombination { // 计算阶乘 public static int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } // 计算排列数 public static int permutation(int n, int r) { return factorial(n) / factorial(n - r); } // 计算组合数 public static int combination(int n, int r) { return permutation(n, r) / factorial(r); } // 递归实现全排列 public static void permutation(List<Integer> nums, int start, List<List<Integer>> result) { if (start == nums.size()) { result.add(new ArrayList<>(nums)); return; } for (int i = start; i < nums.size(); i++) { swap(nums, start, i); permutation(nums, start + 1, result); swap(nums, start, i); } } // 交换数组中两个元素的位置 public static void swap(List<Integer> nums, int i, int j) { int temp = nums.get(i); nums.set(i, nums.get(j)); nums.set(j, temp); } public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3); // 计算排列数和组合数 System.out.println("permutation: " + permutation(5, 3)); System.out.println("combination: " + combination(5, 3)); // 计算全排列 List<List<Integer>> result = new ArrayList<>(); permutation(nums, 0, result); System.out.println("permutation result: " + result); } } ``` 输出结果为: ``` permutation: 60 combination: 10 permutation result: [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2]] ``` 其中,`permutation`和`combination`函数分别计算排列数和组合数,`permutation`函数使用阶乘实现,`combination`函数使用排列数实现。 `permutation`函数使用递归实现全排列,`swap`函数用于交换数组中两个元素的位置。在`main`函数中,我们使用`permutation`函数计算`[1, 2, 3]`的全排列,并将结果存储在`result`变量中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuming先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值