HTML/JavaScript小工具

HTML/JavaScript小工具

2016年6月24日 星期五

數字排列

public class BFS1_6 {
    static int[] arrays = {1,2,3,4,5,6,7};
    static int[] shwoArrays = new int[arrays.length];
    static int[] mark = new int[arrays.length];  
    static int count = 0 ;
    public static void dfs(int step){
        if (step == arrays.length){
            for(int value : shwoArrays){
                System.out.print(value+" ");
            }
            count++;
            System.out.println();
            return;
        }
       
        for (int i = 0;i<arrays.length;i++){
           
            if (mark[i] == 0){
                mark[i] = 1;
                shwoArrays[step] = arrays[i];
                dfs(step + 1);
                mark[i] = 0;
            }
           
        }
       
       
    }
    public static void main(String[] args) {
        // TODO code application logic here
        dfs(0);
        System.out.println(count);
    }

}



排列種類數量
2 2
3 6
4 24
5 120
7 5040
公式
2 * (N1 * N2*N3...)
如要計算
5 的排列種類數量
2 * 3 * 4 * 5 = 120

沒有留言:

張貼留言