java 组合数公式计算_计算一维组合数的java实现

1 /**2 * @author "shihuc"3 * @date 2016年12月1日4 */

5 6

7 import java.util.ArrayList;8 import java.util.Arrays;9

10 /**11 * @author chengsh0512 *13 * 组合算法实现,支持产品列表页的筛选模块实现全静态化。14 *15 * 给定m个不同的数,从中选择出n个数的不同选择方法有多少种?16 * 答案:一共有 n!/(n-m)!*m!17 *18 * 存储计算出来的组合结构,组合元素之间用逗号分隔19 * 例如1,2,3的全组合:20 * "1,", "2,", "3,","1,2,", "1,3,", "2,3,", "1,2,3,"21 */

22 public classCombination {23

24 /**25 * 从m个元素中取出n个元素,获取排序好了的组合数列表,同一个组合中的元素按从小到大排序。26 *27 * @param m 组合的元素基数28 * @param n 组合的被选元素个数29 * @return 排序好后的组合列表30 * @throws Exception31 */

32 public ArrayList getCombinations(int m, intn) throws Exception{33

34 if(m

38 ArrayList resCom =calcCombination(m, n);39

40 returnresCom;41 }42

43 /**44 * 通过移位方式,计算给定m个数中取出n个数的组合列表。45 *46 * 具体思路:47 * 一个bit位(boolean)数组中,初始化全为0(false), 然后给左边的n个位初始化为1(true)。48 * <1> 从左向右找第一个10的位置,将10换位程01,然后将这个01左边的所有的1全都 移位到数组的最左边,此时得到的1所在位置下标对应的序列即为一个组合数。49 * <2> 循环重复上面的<1>步骤的操作,直到所有的1都移动到最右边为止。50 *51 * @param m 输入的基数个数52 * @param n 组合被选元素格式53 * @return 原始组合数列表,即没有排序的组合54 */

55 private ArrayList calcCombination(int m, intn){56 boolean base[] = newboolean[m];57 Arrays.fill(base, false);58 for(int i=0; i

64 private ArrayList combination(boolean a[], int m, intn){65 ArrayList combination = new ArrayList();66 while(!isAllZeroLeft(a, m, n)){67 for(int i=0; i

71 combination.add(elem);72 oneZeroSwap(a, i, i+1); //完成10/01换位

73 moveFrontOneToLeft(a, i); //完成剩余左边的1全向最左边搬移操作

74 break;75 }76 }77 }78

79 //最后一个元素也是组合的一个,即所有的1(true)都到了数组的最右边

80 combination.add(calcElement(a));81 returncombination;82 }83

84 /**85 * 异或操作实现不开辟新的存储空间完成两个数的位置交换。86 *87 * @param a 待操作数所在的数组88 * @param x 待交换位置的第一个数在数组中的下标89 * @param y 待交换位置的第二个数在数组中的下标90 */

91 private void oneZeroSwap(boolean a[], int x, inty){92 a[x] = a[x] ^a[y];93 a[y] = a[y] ^a[x];94 a[x] = a[x] ^a[y];95 }96

97 /**98 * 判断10作01交换位置后,是否实现了数组a中右端的n个元素全为1(true),这个结果作为10/01换位结束标识99 *100 * @param a 10/01换位的输入数组101 * @param m 计算组合的元数据个数102 * @param n 计算组合的被选取元素个数103 * @return true表示10/01换位结束,false表示还可以继续104 */

105 private boolean isAllZeroLeft(boolean a[], int m, intn){106 int gap = m -n;107 for(int i=0; i

115 /**116 * 将10/01换位之后数组左边的全部1都搬移到数组的最左边。117 *118 * @param a 待操作的组合数组119 * @param end 指明搬移操作的范围,在end数组下标左边的进行搬移, 这个end的值小于数组的长度120 */

121 private void moveFrontOneToLeft(boolean a[], intend){122 int oneCnt = 0;123 for(int i=0; i

134 /**135 * 计算当前数组中的组合元素的值,数组元素为1(true)的对应的下标的全集,即为所需的一个组合元素值136 *137 * @param a 待操作的组合数组138 * @return 一个组合的值, 去掉了最后的一个逗号分隔符139 */

140 privateString calcElement(boolean a[]){141 String elem = "";142 for(int i=0; i

150

151 /**152 * @param args153 * @throws Exception154 */

155 public static voidmain(String[] args) throws Exception {156 int m = 5, n = 2;157 Combination combination = newCombination();158 ArrayList coms =combination.getCombinations(m, n);159 for(int i = 0; i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值