输入一组字符串,将其所有的输出形式都显出出来!!
列如:有一组"abc"字符串,它可以有这样的输出形式:abc,acb,bac,bca,cab,cba

解决方案 »

  1.   

    把A B C看成一个全图的3个点就行啦
      

  2.   

    这个我以前做练习做到过
    的和你稍微不一样点  运行结果不是在屏幕上显示,是输出到C:\mytest\permuted.txt 里
    你看懂了自己改下
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.nio.channels.FileChannel;class PermuteWords {  // These are static class members so we can reference them in all the methods in the class
      static String phrase = "a b c";
      static ByteBuffer buffer = null;
      static FileChannel wordsChannel = null;  static String[] words = null;         // Stores reference to array that will hold words from phrase
      static String[] permutation = null;   // Stores reference to array that will hold a permutation of words from the phrase
      static int path = -1;                 // Stores the number of words accumulated in one permutation  public static void main(String args[]) {
        // Extract the words from phrase into an array. 
        // First figure out how many words there are, assuming words are separated by spaces...
        int fromIndex = -1;
        while(phrase.charAt(++fromIndex) == ' ');          // Skip any leading spaces
       
        int wordCount = 1;                                 // At least one word
        while((fromIndex = phrase.indexOf(' ', fromIndex)) != -1) {
          wordCount++;
          // Skip contiguous spaces     
          while(phrase.charAt(++fromIndex) == ' ');
        }
        words = new String[wordCount];                     // Array to hold the words from the phrase
        permutation = new String[wordCount];               // Array to hold permuted words
        
        // Now get the words    
        fromIndex = -1;
        while(phrase.charAt(++fromIndex) == ' ')           // Skip any leading spaces
          ;
        int toIndex = 0;                                   // Word end position
        int index = 0;                                     // Free element in words array
        while((toIndex = phrase.indexOf(' ', fromIndex)) != -1) {
          words[index++] = phrase.substring(fromIndex,toIndex);
          fromIndex = toIndex;
             
          while(phrase.charAt(++fromIndex) == ' ')         // Skip contiguous spaces
            ;
        }
        if(fromIndex<phrase.length()) {
          words[index] = phrase.substring(fromIndex, phrase.length());
        }    // Now create and write the file
        String fileName = "permuted.txt";
        String directoryName = "C://mytest";    // Make sure we have a directory for the file
        File directory = new File(directoryName);
        if(!directory.exists() || (directory.exists() && !directory.isDirectory())) {
          directory.mkdir();
        }
          
        File wordsFile = new File(directory, fileName);                   // Create the output stream for the file
        FileOutputStream wordsOutputStream = null;
        try {
          wordsOutputStream = new FileOutputStream(wordsFile);
        } catch (FileNotFoundException e) {
          e.printStackTrace(System.err);
          System.exit(1);
        }
        wordsChannel = wordsOutputStream.getChannel();
        buffer = ByteBuffer.allocate(2*(phrase.length()+1));  // We add one to allow for '\n' at the beginning
        permute(0);                                           // Create and write all permutations of words array    try {
          wordsOutputStream.close();
        } catch (IOException e) {
          e.printStackTrace(System.err);
          System.exit(1);
        }
      } 
      private static void permute(int n) {
        permutation[n] = ++path == 0 ? null: words[path-1];
        if(path == words.length) { 
           write();
        } else {
          for (int i = 0; i<words.length ; i++) {
            if(permutation[i] == null) {
               permute(i);
            }
          }
        }
          path--;
          permutation[n] = null;
      }  // Writes a permutation to the file
      private static void write() {
        CharBuffer charBuffer = buffer.asCharBuffer();
        for(int i = 0 ; i< permutation.length ; i++) 
          charBuffer.put(i==0?'\n':' ').put(permutation[i]);
       
        try {
          wordsChannel.write(buffer);
        } catch (IOException e) {
            e.printStackTrace(System.err);
            System.exit(1);
        } 
        buffer.clear();
      }
    }
          
        File wordsFile = new File(directory, fileName);                   // Create the output stream for the file
        FileOutputStream wordsOutputStream = null;
        try {
          wordsOutputStream = new FileOutputStream(wordsFile);
        } catch (FileNotFoundException e) {
          e.printStackTrace(System.err);
          System.exit(1);
        }
        wordsChannel = wordsOutputStream.getChannel();
        buffer = ByteBuffer.allocate(2*(phrase.length()+1));  // We add one to allow for '\n' at the beginning
        permute(0);                                           // Create and write all permutations of words array    try {
          wordsOutputStream.close();
        } catch (IOException e) {
          e.printStackTrace(System.err);
          System.exit(1);
        }
      } 
      private static void permute(int n) {
        permutation[n] = ++path == 0 ? null: words[path-1];
        if(path == words.length) { 
           write();
        } else {
          for (int i = 0; i<words.length ; i++) {
            if(permutation[i] == null) {
               permute(i);
            }
          }
        }
          path--;
          permutation[n] = null;
      }  // Writes a permutation to the file
      private static void write() {
        CharBuffer charBuffer = buffer.asCharBuffer();
        for(int i = 0 ; i< permutation.length ; i++) 
          charBuffer.put(i==0?'\n':' ').put(permutation[i]);
       
        try {
          wordsChannel.write(buffer);
        } catch (IOException e) {
            e.printStackTrace(System.err);
            System.exit(1);
        } 
        buffer.clear();
      }
    }
      

  3.   

    晕!复制粘贴是时多复制了一次~看这个
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.nio.channels.FileChannel;class PermuteWords {  // These are static class members so we can reference them in all the methods in the class
      static String phrase = "a b c";
      static ByteBuffer buffer = null;
      static FileChannel wordsChannel = null;  static String[] words = null;         // Stores reference to array that will hold words from phrase
      static String[] permutation = null;   // Stores reference to array that will hold a permutation of words from the phrase
      static int path = -1;                 // Stores the number of words accumulated in one permutation  public static void main(String args[]) {
        // Extract the words from phrase into an array. 
        // First figure out how many words there are, assuming words are separated by spaces...
        int fromIndex = -1;
        while(phrase.charAt(++fromIndex) == ' ');          // Skip any leading spaces
       
        int wordCount = 1;                                 // At least one word
        while((fromIndex = phrase.indexOf(' ', fromIndex)) != -1) {
          wordCount++;
          // Skip contiguous spaces     
          while(phrase.charAt(++fromIndex) == ' ');
        }
        words = new String[wordCount];                     // Array to hold the words from the phrase
        permutation = new String[wordCount];               // Array to hold permuted words
        
        // Now get the words    
        fromIndex = -1;
        while(phrase.charAt(++fromIndex) == ' ')           // Skip any leading spaces
          ;
        int toIndex = 0;                                   // Word end position
        int index = 0;                                     // Free element in words array
        while((toIndex = phrase.indexOf(' ', fromIndex)) != -1) {
          words[index++] = phrase.substring(fromIndex,toIndex);
          fromIndex = toIndex;
             
          while(phrase.charAt(++fromIndex) == ' ')         // Skip contiguous spaces
            ;
        }
        if(fromIndex<phrase.length()) {
          words[index] = phrase.substring(fromIndex, phrase.length());
        }    // Now create and write the file
        String fileName = "permuted.txt";
        String directoryName = "C://mytest";    // Make sure we have a directory for the file
        File directory = new File(directoryName);
        if(!directory.exists() || (directory.exists() && !directory.isDirectory())) {
          directory.mkdir();
        }
          
        File wordsFile = new File(directory, fileName);                   // Create the output stream for the file
        FileOutputStream wordsOutputStream = null;
        try {
          wordsOutputStream = new FileOutputStream(wordsFile);
        } catch (FileNotFoundException e) {
          e.printStackTrace(System.err);
          System.exit(1);
        }
        wordsChannel = wordsOutputStream.getChannel();
        buffer = ByteBuffer.allocate(2*(phrase.length()+1));  // We add one to allow for '\n' at the beginning
        permute(0);                                           // Create and write all permutations of words array    try {
          wordsOutputStream.close();
        } catch (IOException e) {
          e.printStackTrace(System.err);
          System.exit(1);
        }
      } 
      private static void permute(int n) {
        permutation[n] = ++path == 0 ? null: words[path-1];
        if(path == words.length) { 
           write();
        } else {
          for (int i = 0; i<words.length ; i++) {
            if(permutation[i] == null) {
               permute(i);
            }
          }
        }
          path--;
          permutation[n] = null;
      }  // Writes a permutation to the file
      private static void write() {
        CharBuffer charBuffer = buffer.asCharBuffer();
        for(int i = 0 ; i< permutation.length ; i++) 
          charBuffer.put(i==0?'\n':' ').put(permutation[i]);
       
        try {
          wordsChannel.write(buffer);
        } catch (IOException e) {
            e.printStackTrace(System.err);
            System.exit(1);
        } 
        buffer.clear();
      }
    }
      

  4.   

    想到一个挺有意思的解法:假如有30个字符, 那么用random(1,30)生成一个随机数,循环30次,就有30个随机数,每个随机数对应一个字符串数组下标的话,就可以生成一个随机的字符串,我把这叫做一个生成。新字符串里面的元素都来自于原来的字符串。有两个问题:1,在一个生成里,让这30个随机数指向不同的字符;2,所有的生成要囊括所有的可能。对第一个问题, 可以借用hash表分配的方法,比如构造一个函数,如果有重复生成的随机数,用函数再分配,f(x) = x+1, f(x) = x-1 之类简单的函数, 比如再来一个随机函数, 比如当遇到重复的时候就从字符串开始处选择一个来填充。对第二个问题,生成的随机数基本上是服从正态分布的,就是说中间生成(15左右)的数多,两边生成的数少,这样就会导致以中间字符开头的生成偏多,根据正态分布的规律,如果进行(30/1.414)次生成的话,就能覆盖所有的生成。 PS:1.414这个数据我不敢确定。呵呵,写完了,好复杂。 因为最近学了random,有用它的冲动。
      

  5.   

    这个问题好像在 core java II 中有这么一段代码,就是实现楼上的这个功能的.很短.
      好像在讲JList 时讲的, 具体怎么写我也也忘了, 建议参考一下哈
      

  6.   

    楼上的代码把我弄晕了,楼主是说任意输入一字符串,如果是abc这么简单的还用得着编程序吗
      

  7.   

    ABC不也是一个字符串嘛 ABC懂了 其他就可以举一反三了嘛
      

  8.   

    刚编了一个迭代的/*
    输入一组字符串,将其所有的输出形式都显出出来!!
    列如:有一组"abc"字符串,它可以有这样的输出形式:abc,acb,bac,bca,cab,cba
     *
     *
     迭代原理:
     1.abc
     2.(ab)c, (ac)b,(bc)a//把最后一个元素和这个元素前面的元素一一交换
     3.(a,b)c, (b,a)c,(a,c)b,(c,a)b,  (b,c)a, (c,b)a.把倒数第二个元素和他前面的元素一一交换
     4.迭代,直到要交换的元素是第一个元素
     */package stringderi;import java.util.ArrayList;public class Main {
        
        static String origin = "abcd";
        
        public static void main(String[] args) {
          
           int i,j;
           int count = origin.length();
           ArrayList current = new ArrayList();
           ArrayList next = new ArrayList();
           
           current.add(origin);//作为第一次循环的初始值
           
           while(count !=1 ){//还没有到第一个元素
               for(i=0;i<current.size();i++){
                   for(j=0;j<count;j++){
                        next.add(exchange((String)current.get(i), j, count-1)); 
                        //把要交换的元素(由count指示)和每一个前面的元素(j指示)进行交换
                   }                   
               }
               current.clear();
               current.addAll(next);
               next.clear();
               count--;
           }//while
           for(i=0;i<current.size();i++){
               System.out.println((String)current.get(i));
           }
        }
        
       public static String exchange(String s, int i, int j){//交换i,j位置的字符
           char c1='a';
           char c2='a';
           
           StringBuffer buffer = new StringBuffer(s);
           if(i<s.length())
               c1 = s.charAt(i);
           if(j<s.length())
               c2 = s.charAt(j);
           
           buffer.setCharAt(i,c2);
           buffer.setCharAt(j,c1);
           return buffer.toString();
         
           
       }
        
    }
      

  9.   

    我编的是不是慢了点? 看我前后回复的时间差是1小时24分, 即使中间有时间浪费,也要一个小时左右。???这个是什么意思?
    --------------------------------------------------------
       回复人:druglee() ( 一级(初级)) 信誉:100  2006-11-12 19:01:49  得分:0

    把A B C看成一个全图的3个点就行啦
    -------------------------------------------------------------
      

  10.   

    回lovelyhermione() ( ) 
    任意字符串你不会把他们拆开啊~,在组成合适的字符串形式,做人要变通
      

  11.   

    昨天发出的帖子今天才回,以为大家会想点方便的方法呢,没有想到都蛮复杂的!!郁闷!!!
    这是我自己想的一点思路不知道能不能有人能完善它:
    就是定义一个数组ARRAY,让数组中的ARRAY[0]与ARRAY[1]交换在用 for(int element;ARRAY) System.out.println(element);将其输出,再将交换后的数组的AYYAY[1]与ARRAY[2]交换再用
    for(int element;ARRAY) System.out.println(element);将其输出,依次类推,直到
    ARRAY[ARRAY.lenth-2]与ARRAY[ARRAY.lenth-1]交换,这样交换完以后再从前向后在交换^^^^^^^^^^当然这其中要用到循环语句!!!
    请大家看看怎么样!过程大概是这样的,
    就拿abc为列吧!
    首先将a与b交换再输出即为:bac,再将a与c交换再输出即为;bca(这是第一次用a交换完)
    将b与c交换再输出即为;cba,再将b与a交换输出;cab(这是第二次用b交换)
    将c与a交换输出;acb,再将c与b交换输出;abc(这是第三次用c交换)
    最后各种可能都输出了!!
    但好象还是有问题呢!!
    你们帮我看看吧
      

  12.   

    敢问一个问题  如果输入  AABBD  就是输入的有重复的字符 
    各位 如果打出来的全排  肯定会有重复的 
    请问这个问题 怎么解决?
      

  13.   

    我的想法是将字符串转换成字符数组。通过字符数组下标,转换成数学问题,如:"abc",对应的下标为0,1,2然后在排序(这样可以多加上数学算法计算的方法,还可以解决字符串中存在重复的问题)。排好后讲下标对应的字符组合起来。但是我木想出来解法,还是学的东西少哈。。555555