全排列算法 java 实现 全排列算法 java 实现

/*
* Created on Oct 22, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/

/**
* @author WangBNU
*    helloquota@163.com
* My Homepage  http://bnulinux.yeah.net
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class PaiLie {
    public static void main(String args[]) {
        int leng = args[0].length();
        int[] a = new  int[leng];
        boolean[] c = new boolean[leng];
        for (int i = 0; i < leng; i++) {
            a[i] = i + 1;
            c[i] = false;  
        }
        int ii = findActiveM(a, c);

        for (int i = 0; i < args[0].length(); i++)
        System.out.print(a[i]);
        System.out.println( ) ;

        if (ii >= 0) {
            do {
                int activeM= a[ii] ;
                if (c[ii] == false) {
                    swapInt(a,ii, ii - 1);
                    swapBoolean(c,ii, ii - 1);
                } else {
                    swapInt(a,ii, ii + 1);
                    swapBoolean(c,ii, ii + 1);
                }
                for (int i = 0; i < args[0].length(); i++) {
                    System.out.print(a[i]);
                    if (a[i] > activeM)
                        c[i] = !c[i];
                }
                System.out.println();
                ii = findActiveM(a, c);


            } while (ii >= 0);
        }
    }


    /**
    * @param aa   
    * @param a
    * @param b
    */
    private static void swapInt(int[] aa , int a, int b) {
        int temp = aa[a];
        aa[a] = aa[b];
        aa[b] = temp;
    }

    private static void  swapBoolean(boolean[] aa , int  a , int b) {
        boolean temp = aa[a];
        aa[a] = aa[b];
        aa[b] = temp;
    }
    private static int findActiveM(int[] a, boolean[] c) {

        int first1 = 0, first2 = 0;
        int m = -1, n = -1;
        for (int i = 1; i < a.length - 1; i++) {
            if (c[i] == false) {
                if ((a[i] > a[i - 1]) && a[i] > first1) {
                    first1 = a[i];
                    m = i;
                }
            } else {
                if ((a[i] > a[i + 1]) && a[i] > first2) {
                    first2 = a[i];
                    n = i;
                }
            }
        }
        if ((c[0] == true) && (a[0] > a[1]) && (a[0] > first2)) {
            first2 = a[0];
            n = 0;
        }
        if ((c[a.length - 1] == false) && (a.length>1)
                && (a[a.length - 1] > a[a.length - 2])
                && (a[a.length - 1] > first1)) {
            first1 = a[a.length - 1];
            m = a.length - 1;
        }
        if (first1 >= first2)
            return m;
        else
            return n;
    }
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值