-3

我想制作一个程序来处理所有长度小于我要指定的数字的字符串。我想要这样的东西:

for(int char1 = 32; char1 <= 126; char1++){
  for(int char2 = 32; char2 <= 126; char2++){
    char[] chars = new char[2];
    char[0] = (char)char1; char[1] = (char)char2;
    String s = String.valueOf(chars);
    doStuffWith(s);
  }
}

但当然有可选择数量的字符。这可能吗?

它不是用于暴力破解密码,而是用于浏览服务器上的文件。

4

2 回答 2

1

这个怎么样?非递归解决方案。

int min = 32;
int max = 127;
int numChars = 6; //Set to whatever value you like.
int numDifferentChars = max - min;
int numPossibilities = (int)Math.pow(max - min,numChars);
for(int i = 0;i < numPossibilities;i++) {
    char[] chars = new char[numChars];
    for(int j = 0;j < numChars;j++) {
        int chr = i;
        for(int k = 0;k < j;k++) {
            chr /= numDifferentChars;
        }
        chars[j] = (char)((chr % numDifferentChars) + min);
    }
    doStuffWith(String.valueOf(chars));
}

我猜它可以提高效率,但这似乎有效,除非 numPossibilities 有整数溢出。

编辑:此代码有效。我测试过了!

于 2013-10-05T21:14:58.647 回答
0

这比较了使用循环和递归。我一般不喜欢 Java 中的递归,但在这种情况下,它更干净、更快。

private static volatile int dontOptimiseAway;

public static void main(String[] args) throws IOException, ClassNotFoundException {
    for (int i = 1; i <= 6; i++) {
        long comb = testCombinations(i, 'a', 'z');
        long iter = testIteration(i, 'a', 'z');
        System.out.printf("%d: Comb: %.3f secs, loops: %.3f%n", i, comb / 1e9, iter / 1e9);
    }
}

private static long testCombinations(int maxLetters, char min, char max) {
    long start = System.nanoTime();
    for (int i = 1; i <= maxLetters; i++)
        combinations(i, min, max);
    return System.nanoTime() - start;
}

public static void combinations(int length, char min, char max) {
    combinations0(new char[length], 0, min, max);
}

private static void combinations0(char[] chars, int i, char min, char max) {
    if (i >= chars.length) {
        doStuff(chars);
    } else {
        for (char ch = min; ch <= max; ch++) {
            chars[i] = ch;
            combinations0(chars, i + 1, min, max);
        }
    }

}

private static void doStuff(char[] chars) {
    dontOptimiseAway = chars.length;
}

private static long testIteration(int maxLetters, char min, char max) {
    long start = System.nanoTime();
    for (int i = 1; i <= maxLetters; i++)
        loops(i, min, max);
    return System.nanoTime() - start;
}

public static void loops(int length, char min, char max) {
    int numDifferentChars = max - min + 1;
    long numPossibilities = (long) Math.pow(numDifferentChars, length);
    char[] chars = new char[length];
    for (long i = 0; i < numPossibilities; i++) {
        long chr = i;
        for (int j = 0; j < length; j++) {
            chars[j] = (char) (chr % numDifferentChars + min);
            chr /= numDifferentChars;
        }
        doStuff(chars);
    }
}

印刷

1: Comb: 0.000 secs, loops: 0.000
2: Comb: 0.000 secs, loops: 0.000
3: Comb: 0.003 secs, loops: 0.004
4: Comb: 0.006 secs, loops: 0.025
5: Comb: 0.116 secs, loops: 0.793
6: Comb: 3.856 secs, loops: 25.101
于 2013-10-05T21:53:14.580 回答