实现组合
简书不支持代码格式,蛋疼。。。。
要求梗概:堆场5排,4层高,放有次序的20个箱子进去,求有多少种方法。
那么也就是每个箱子都可以标号1-5,代表放的排的位置。
大致思路可以表示如下。deg【i】代表i排的箱子数目

就是一个递归思路,注意最后还是要deg[i]++,因为如果不++,递归套一层出来后所有箱子信息就是0了,这样就只有一个结果了。
import java.util.ArrayList;
import java.util.List;
/**
* Created by LeeTom on 2018/1/29.
*/
public class traversal {
public static final int height =4;//层高;
public static final int row =5;//排数
public static int []count =new int[]{height,height,height,height,height,height};//1*6的数组
public static int total =0;//总数
private static int m =height*row;
public static void main(String[] arg){
List L1 =new ArrayList<>();
new traversal().findall(L1,m);
System.out.println("count: " +total);
}
private void findall( List L1,int m){
if(m ==0){
System.out.println();*/
total++;
return;
}
List L2;
for(int i=1;i<=row;i++){
L2 =new ArrayList();
L2.addAll(L1);
if(count[i]>0){
L2.add(i);
count[i]--;
findall(L2,m-1);
count[i]++;
}
}
}
}