1. 程式人生 > 實用技巧 >排列組合問題

排列組合問題

public class CommonTest {

//    public static void main(String[] args) {
//        perm(new int[]{1,2,3},new Stack<>());
//    }
//    public static void perm(int[] array, Stack<Integer> stack) {
//        if(array.length <= 0) {
//            //進入了葉子節點,輸出棧中內容
//            System.out.println(stack);
//        } else {
//            for (int i = 0; i < array.length; i++) {
//                //tmepArray是一個臨時陣列,用於就是Ri
//                //eg:1,2,3的全排列,先取出1,那麼這時tempArray中就是2,3
//                int[] tempArray = new int[array.length-1];
//                System.arraycopy(array,0,tempArray,0,i);
//                System.arraycopy(array,i+1,tempArray,i,array.length-i-1);
//                stack.push(array[i]);
//                perm(tempArray,stack);
//                stack.pop();
//            }
//        }
//    }
//============================================================================
//    private static int cnt = 0;
//    public static void main(String[] args) {
//        perm(new int[]{1,2,3,4},0,3);
//        System.out.println(cnt);
//    }
//
//    /**
//     * @param array
//     * @param start
//     * @param end  這個end不是目標層級,是陣列內元素交換的閾值,一直交換到資料末端
//     */
//    public static void perm(int[] array,int start,int end) {
//        if(start==end) {
//            System.out.println(Arrays.toString(array));
//            cnt++;
//
//        } else {
//            for (int i = start; i <= end; i++) {
//                //1,2,3的全排列這塊相當於將其中一個提了出來,下次遞迴從start+1開始
//                swap(array,start,i);
//                perm(array,start+1,end);
//                //這塊是復原陣列,為了保證下次另外的同級遞迴使用陣列不會出錯
//                //這塊可以通過樹來理解,每次回退一步操作,交換回去
//                swap(array,start,i);
//            }
//        }
//    }
//    public static void swap(int[] array,int i,int j) {
//        int temp = array[i];
//        array[i] = array[j];
//        array[j] = temp;
//    }
//=============================================================================
    /**
     * 第一個問題:
     *
     *   首先,先讓我們來看第一個問題, 有1,2,3,4這4個數字.可以重複的在裡面選4次,問能得到多少種結果.
      */
//
//static int cnt = 0;
//public static Stack<Integer> stack = new Stack<Integer>();
//    public static void main(String[] args) {
//        int shu[] = {1,2,3};
//        f(shu,3,0);//這裡是4不是3,如果3,輸出[1, 1, 1]
//        System.out.println(cnt);
//    }
//    /**
//     *
//     * @param shu   待選擇的陣列
//     * @param targ  目標組合層級, 這裡給定之後,值就是是固定的,給定層級為3,那麼輸出三個數字
//     * @param cur   當前選擇的是第幾級
//     */
//    private static void f(int[] shu, int targ, int cur) {
//        if(cur == targ) {
//            System.out.println(stack);
//            cnt++;//統計總條數
//            return;
//        }
//
//        for(int i=0;i<shu.length;i++) {
//            stack.add(shu[i]);
//            f(shu, targ, cur+1);//cur就是用來控制樹的層級,深度
//            /**
//             *           1           2         3        cur=0
//             *        [1 2 3]     [1 2 3]   [1 2 3]     cur=1
//             *    [123][123][123]      ......           cur=2
//             *    當cur=3時,等於tag,那麼開始列印stack,然後開始回溯到cur=2級,index加1,直到shu大小,之後回溯到cur=1級別,繼續指index加1.....
//             */
//            stack.pop();//棧用來回溯
//
//        }
//    }
//
//==================================================================================
//    static int cnt = 0;
//    static Stack<Integer> s = new Stack<Integer>();
//    static boolean[] used = new boolean[10000];
//
//    /**
//     * 遞迴方法,當實際選取的小球數目與要求選取的小球數目相同時,跳出遞迴
//     * @param minv - 小球編號的最小值
//     * @param maxv - 小球編號的最大值
//     * @param curnum - 當前已經確定的小球的個數
//     * @param maxnum - 要選取的小球的數目
//     */
//    public static void kase2(int minv,int maxv,int curnum, int maxnum){
//        if(curnum == maxnum){
//            cnt++;
//            System.out.println(s);
//            return;
//        }
//
//        for(int i = minv; i <= maxv; i++){
//            if(!used[i]){
//                s.push(i);
//                used[i] = true;
//                kase2(minv, maxv, curnum+1, maxnum);
//                s.pop();
//                used[i] = false;
//            }
//        }
//    }
//
//    public static void main(String[] args){
//        int shu[] = {1,2,3,4};
//        //kase2(shu[], 0, 3);
//        System.out.println(cnt);
//    }
//========================================================

}