本文实例为大家分享了java排列组合算法的具体代码,供大家参考,具体内容如下
package BeanUtil; import java.util.ArrayList; import java.util.List; import com.work.core.exception.OurException; /** * 统计任三出现的最多的几率的组合 * * @author wangmingjie * @date 2009-1-1下午01:22:19 */ public class Copy_2_of_StatisAnyThree { // 组合算法 // 本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标 // 代表的数被选中,为0则没选中。 // 首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。 // 然后从左到右扫描数组元素值的“10”组合,找到第一个“10”组合后将其变为 // “01”组合,同时将其左边的所有“1”全部移动到数组的最左端。 // 当第一个“1”移动到数组的m-n的位置,即n个“1”全部移动到最右端时,就得 // 到了最后一个组合。 // 例如求5中选3的组合: // 1 1 1 0 0 //1,2,3 // 1 1 0 1 0 //1,2,4 // 1 0 1 1 0 //1,3,4 // 0 1 1 1 0 //2,3,4 // 1 1 0 0 1 //1,2,5 // 1 0 1 0 1 //1,3,5 // 0 1 1 0 1 //2,3,5 // 1 0 0 1 1 //1,4,5 // 0 1 0 1 1 //2,4,5 // 0 0 1 1 1 //3,4,5 public static void main(String[] args) { Copy_2_of_StatisAnyThree s = new Copy_2_of_StatisAnyThree(); s.printAnyThree(); } /** * */ public void printAnyThree(){ int[] num = new int[]{1,2,3,4,5,6}; print(combine(num,3)); } /** * 从n个数字中选择m个数字 * @param a * @param m * @return */ public List combine(int[] a,int m){ int n = a.length; if(m>n){ throw new OurException("错误!数组a中只有"+n+"个元素。"+m+"大于"+2+"!!!"); } List result = new ArrayList(); int[] bs = new int[n]; for(int i=0;i
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。