public class Combination extends Object implements Serializable
| Constructor and Description |
|---|
Combination(String[] datas)
组合,即C(n, m)
排列组合相关类 参考:http://cgs1999.iteye.com/blog/2327664 |
| Modifier and Type | Method and Description |
|---|---|
static long |
count(int n,
int m)
Deprecated.
|
static long |
countAll(int n)
计算组合总数,即C(n, 1) + C(n, 2) + C(n, 3)...
|
static BigInteger |
countBig(int n,
int m)
计算组合数 C(n, m) 的 BigInteger 精确版本。
|
static long |
countSafe(int n,
int m)
安全组合数 long 版本。
|
List<String[]> |
select(int m)
组合选择(从列表中选择m个组合)
|
List<String[]> |
selectAll()
全组合
|
public Combination(String[] datas)
datas - 用于组合的数据@Deprecated public static long count(int n, int m)
注意:此方法内部使用 BigInteger 修复了旧版 factorial 的计算错误, 但最终仍以 long 返回,因此当结果超过 long 范围时仍会溢出。
建议使用 countBig(int, int) 获取精确结果,或使用
countSafe(int, int) 获取安全 long 版本。
n - 总数m - 选择的个数public static BigInteger countBig(int n, int m)
数学定义: C(n, m) = n! / (m! (n - m)!)
优化方式: 1. 利用对称性 m = min(m, n-m) 2. 每一步先乘 BigInteger,再除以当前 i,保证数值不暴涨
n - 总数 n(必须 大于等于 0)m - 取出 m(必须 大于等于 0)public static long countSafe(int n,
int m)
n - 总数 n(必须 大于等于 0)m - 取出 m(必须 大于等于 0)
若结果超出 long 范围,会抛 ArithmeticException,而非溢出。
public static long countAll(int n)
n - 总数Copyright © 2025. All rights reserved.