代码之家  ›  专栏  ›  技术社区  ›  joshkmartinez

在Java中输出数字和字母的所有可能组合

  •  -5
  • joshkmartinez  · 技术社区  · 7 年前

    我知道已经有很多类似的问题存在,但没有一个能解决我的问题。

    给定一个字符数组,我希望能够 我想用这些字符生成一个特定长度的所有可能组合的列表。对于列表中的每一项,我都希望将其写入输出文件中的单独一行。
    请注意,此代码应用于 大的 一组数字。(我想在36个字符的数据集上使用它,并希望生成32个长字符串)我知道有6.3340286662E+49个可能的答案,并希望此函数生成 所有这些 .
    下面是一些输入/输出示例:
    输入:
    int[] a ={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0'};
    calc(a,32);

    输出文件:

    457abe9e7d27439681d62f4e0de1f5e1  
    4adaw435kj546bjk34k4j234kj23f7t3  
    awdf5e13h4kj546j43k13i3kj24b32hj
    12ibj3jk2b4kj23b4kj23b432kjb4uui
    etc..  
    

    我真不知道该如何构造算法 到目前为止,这是我的代码,我知道它不多,我将在处理它时附加更多的代码:

    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.util.Random;
    
    public class generator {
        public static void gen(int[] i, int l) {
            FileWriter fileWriter = new FileWriter("out.txt");
            PrintWriter printWriter = new PrintWriter(fileWriter);
            //printWriter.print("Some String");
            boolean gotAll = false;
            Random rand = new Random();
            while (!gotAll) {
                String newStr = "";
                //not the best way to algorithmically get all possible outcomes
                for (int y = 0; y < l; y++) {
                    //randomly generating characters from array
                    newStr += i[rand.nextInt(i.length)];
                }
                //need to check for duplicate generation
                printWriter.println(newStr);
            }
        }
    
        public static void main(String[] args) {
            int[] i = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
            try {
                gen(i, 32);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
    

    好吧,我能想到的获得所有可能结果的最好方法就是一直随机生成数字,直到找到所有的数字。这显然是非常耗费时间和资源的(解决方案将不可避免地如此)。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Black Arrow    7 年前
    public class A
    {
        private static char[] alphabet = {'a', 'b', 'c', '1', '2'};
        private static StringBuilder partialSolution = new StringBuilder();
        private static void bt(int maxIndex, int index)
        {
        if( index == maxIndex )
            {
                String solution = partialSolution.toString();
                System.out.println(solution);
            }
        else
            {
            for(char c: alphabet)
                {
                    // Extend partial solution
                    partialSolution.append(c);
    
                    bt(maxIndex, index+1);
                    final int lastCharIndex = partialSolution.length()-1;
    
                    // Backtrack
                    partialSolution.deleteCharAt(lastCharIndex);
                }
            }
        }
        private static void btCaller(int maxIndex)
        {
            bt(maxIndex, 0);
        }
        public static void main(String[] args)
        {
            btCaller(3);
        }
    }
    

    这可以通过 Backtracking . 你可以放心,你活不到能看见的程度 this end . D