2 回答

TA贡献1848条经验 获得超10个赞
您可以简单地看到您的转换就像转换到另一个基数(使用前导零始终获得 6 位数字),其中数字映射到您想要的字符。第一个数字具有不同的基数以及要映射到的其他字符。
所以这是我的建议:
private static final char[] first = { '2', '3', '4', '5', '6', '7', '8', '9' };
private static final char[] notFrist = { 'b', 'c', 'd', 'g', 'h', 'j', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v',
'w', 'z', '2', '3', '4', '5', '6', '7', '8', '9' };
public static void main(final String[] args) throws InterruptedException {
final char[] result = new char[6];
int val = 32767;
while (true) {
int remainder = val;
// calculate the last 5 digits and map them to the desired characters
for (int i = 5; 0 < i; i--) {
result[i] = notFrist[remainder % notFrist.length];
remainder /= notFrist.length;
}
if (first.length <= remainder) {
throw new RuntimeException("We cannot convert values >=" + val);
}
// calculate the first (=left most) digit and map them to the desired characters
result[0] = first[remainder];
System.out.println(new String(result));
val++;
}

TA贡献1883条经验 获得超3个赞
嗯,为什么不这样做:
public void createSequence() {
final String ALPHA = "bcdghjlmnpqrstvwz23456789";
final String NUMBER = "23456789";
ArrayList<String> combinations = new ArrayList<>();
for (int l1 = 0; l1 < NUMBER.length(); l1++) {
StringBuilder sequence = new StringBuilder();
sequence.append(NUMBER.charAt(l1));
for (int i = 0; i < 5; i++) {
for (int l2 = 0; l2 < ALPHA.length(); l2++) {
sequence.append(ALPHA.charAt(l2));
}
}
combinations.add(sequence.toString());
}
}
这只会将完整的序列放入列表中。
或者您可以从索引中获取 String :
int number = index+offset;
int digit5 = number%ALPHA.length();
int digit4 = (number-digit5)/ALPHA.length()%ALPHA.length();
...
偏移量是必需的,否则它会包含类似(“b”或“1”)之类的字符串,并且应该类似于: Math.pow(ALPHA.length(),5);
然后您还可以将索引映射到您想要的任何数字方案。
添加回答
举报