编程题:让用户输入任意个字符,然后打印出输入字符的所有可能组合
例如:输入a,b,c
则打印出如下结果:
abc
acb
bac
bca
cab
cba这类程序属于什么算法问题,通用的思路是怎样的,谢谢!

解决方案 »

  1.   

    属于组合数学,有专门的组合算法解决试试这个,http://www.merriampark.com/perm.htm
      

  2.   

    上次某人发的代码 被偶copy了 呵呵  /**
         * @param num how many characters in one result
         * @param array range of the alphabets
         * @return all sequences
         */
        public static String[] getAllSequence(int num, String[] array) {
            if (num == 1) {
                return array;
            }
            else {
                String[] currentArray = getAllSequence(num - 1, array);
                String[] newArray = new String[array.length * currentArray.length];
                for (int i = 0; i < array.length; i++) {
                    for (int j = 0; j < currentArray.length; j++) {
                        newArray[i * currentArray.length + j] = array[i] + currentArray[j];
                    }
                }
                return newArray;
            }
        }
      

  3.   

    采用位运算的思路
    我做过一个类似的例子,你可以看一下    /*求
         *k元集的
         *所有子集划分
         *思想:k项集的子集个数为2^k个,从直观上来看就是
         *k个比特位上,每一位不是0就是1,
         *按照此思路并结合编程语言的位操作,便能产生简洁的算法程序
         */
        public Hashtable<ItemSet, ItemSet> getAllSub() {
            //初始化结果集合,用于存放结果
            Hashtable<ItemSet, ItemSet> res = new Hashtable<ItemSet, ItemSet>();
            //依次生成每一个子集
            int length = items.size();
            for (int i = 1; i < (1 << length) - 1; i++) {
                //左部项目集合
                TreeSet<String> item_left = new TreeSet<String>();
                //右部项目集合(备份原来数据)
                Vector<String> item_right = new Vector<String>(items);
                for (int j = length - 1; j >= 0; j--) {
                    if ((i & (1 << j)) != 0) {
                        item_left.add(items.get(j));
                        item_right.remove(j);
                    }
                }
                ItemSet left = new ItemSet(item_left);
                ItemSet right = new ItemSet(item_right);
                //添加结果
                res.put(left, right);
            }
            return res;
        }
      

  4.   

    这样也可以
    [code]
    public class test{
      String[] list;
      StringBuffer sb = new StringBuffer();
      int start;
      test(String in){
        list = in.split(",");
    printList();
    System.out.print(sb);
      }
      void replaceStr(int c1,int c2){
        String temp = list[c1];
        list[c1] = list[c2];
        list[c2] = temp;
      }
      void printList(){
        if(start==list.length-1)
          for(int i=0;i<list.length;i++)
            sb.append(list[i]+(i==list.length-1?"\n":","));
        else
          for(int i=start;i<list.length;i++){
            replaceStr(start++,i);
            printList();
            replaceStr(i,--start);
          }
      }
      public static void main(String args[]){
        new test("1,2,3,4,5,6");
      }
    }[/code]
      

  5.   

    public class test{
      String[] list;
      StringBuffer sb = new StringBuffer();
      int start;
      test(String in){
        list = in.split(",");
    printList();
    System.out.print(sb);
      }
      void replaceStr(int c1,int c2){
        String temp = list[c1];
        list[c1] = list[c2];
        list[c2] = temp;
      }
      void printList(){
        if(start==list.length-1)
          for(int i=0;i<list.length;i++)
            sb.append(list[i]+(i==list.length-1?"\n":","));
        else
          for(int i=start;i<list.length;i++){
            replaceStr(start++,i);
            printList();
            replaceStr(i,--start);
          }
      }
      public static void main(String args[]){
        new test("1,2,3,4,5,6");
      }
    }
      

  6.   

    楼主问的字符串的全排列,九楼的朋友写的是子集,数量不一样,一个是n!,一个是2的N次幂。11楼的答案是对的,不过顺序不是严格楼主想要的,可以加个排序的过程。
    关于全排列,有它的字典算法,具体内容上网google就行了。