Mala chybicka, zapomnel jsem zapsat posledni davku kombinaci. Oprava:
public static void main(String[] args) {
var chars = "abcdefghijklmnop".toCharArray();
int variations = 7;
var length = chars.length;
var list = new LinkedList<String>();
StringBuilder out = new StringBuilder();
for (int i = 0; i < variations; i++) out.append(chars[0]);
top:
while (true) {
list.add(out.toString());
if (list.size() % 10000 == 0) {
append(list);
list.clear();
}
for (int pos = variations - 1; pos >= 0; pos--) {
char c = out.charAt(pos);
int cPos = Arrays.binarySearch(chars, c);
if (cPos < length - 1) {
// We can just increment this one and we're done
out.setCharAt(pos, chars[cPos + 1]);
continue top;
} else {
// 9-> 10: Every 'lower' digit is reset to first (0).
out.setCharAt(pos, chars[0]);
}
}
break;
}
append(list);
}
public static void append(List<String> combinations) {
try {
Files.write(Paths.get("combinations.txt"), String.join(System.lineSeparator(), combinations).getBytes(),
StandardOpenOption.APPEND, StandardOpenOption.CREATE);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Ted uz budou v souboru vsechny kombinace...