给你 几个 字母  求它的全排列 ~~!比如说 abcd 求他们的全排列!

解决方案 »

  1.   


    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);
            }
        }
        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;
        }
    }  
    看看这个keyibu  ?
      

  2.   

    是不是就是abcd acbd acdb...
      

  3.   

    哥们  别去 copy 阿 ~~ 你给那一大段 看不懂 没用比如说 给abc  
    要求输出如下:
    abc acb bac bca cab abc
      

  4.   

      acb  abc  bca  bac  cba  cab
      

  5.   

    这个可行 但是我 看不懂 
    哪位 高手 解释一下行吗???
    请看public class QuanPaiLei { public static int MAX = 3; public static boolean state[] = new boolean[MAX + 1]; public static int item[] = new int[MAX + 1]; public static String element[] = { "a", "b", "c" }; public static void main(String[] args) {
    DoPermutation(1);
    } public static void DoPermutation(int pos) {
    if (pos > MAX) {
    for (int j = 1; j <= MAX; j++)
    System.out.print(element[item[j] - 1]);
    System.out.println();
    return;
    }
    for (int i = 1; i <= MAX; i++) {
    if (!state[i]) {
    state[i] = true;
    item[pos] = i;
    DoPermutation(pos + 1);
    state[i] = false;
    }
    }
    }
    }
      

  6.   

    我用debug  一步一步观察都 看晕了
      

  7.   


    import java.util.HashSet;public class Queque { /**
     * 对字符串queStr按照字母进行全排列
     * 
     * @param queStr String 需要排列的字符串
     * @param outStr String 已经出列的字符串(初始时为"")
     * @return queuesRs HashSet 其元素为String,保存全排列后的结果
     */
    public HashSet makeQueues(String outStr, String queStr)
    {
    HashSet queuesRs = new HashSet();
    if(queStr.length()==1)
    {
    queuesRs.add(outStr+queStr);
    queuesRs.add(queStr+outStr);
    }
    else if(queStr.length()==2)
    {
    queuesRs.add(outStr+queStr.charAt(0)+queStr.charAt(1));
    queuesRs.add(outStr+queStr.charAt(1)+queStr.charAt(0));
    }
    else
    {
    for(int i=0;i<queStr.length();i++)
    {
    //将要排列的字符串的每个字符分别出列,出列字符追加到原已出列字符串中,同时对新出列字符串和剩余需全排列字符串进行递归
    queuesRs.addAll(makeQueues(outStr+queStr.charAt(i),this.remove(queStr,i)));
    }
    }
    return queuesRs;
    }

    /*
     * 删除字符串中指定索引处的字符,返回删除后的新字符串
     */
    String remove(String str,int idx)
    {
    return str.substring(0,idx)+str.substring(idx+1);
    }
    public static void main(String[]args)
    {
    Queque testQue = new Queque();
    String testStr = "abc";
    HashSet queuesRs = testQue.makeQueues("", testStr);
    System.out.println("全排列结果:"+queuesRs);
    System.out.println("全排列打印数量:"+queuesRs.size());
    int num=1;
    for(int i=1;i<=testStr.length();i++)
    {
    num*=i;
    }
    System.out.println("实际全排列数量:"+num);
    }}
    -------------------------------------------------------------------------------------
    全排列结果:[bca, acb, bac, cab, cba, abc]
    全排列打印数量:6
    实际全排列数量:6