首页 > 解决方案 > 如何生成具有第一个字符串的增加的字母数字应该是数字但是其余字符串长度为 6 的字母数字序列

问题描述

我想生成字母数字递增序列,其中第一个字符串值应该是数字 ie (2,3,4,5,6,7,8,9),接下来的五个字符串可以是字母数字 ie (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)

序列应该从开始1,它应该产生最大可能的组合。这是示例:2bbbbc, 2bbbbd, ..., 2bbbbz, 2bbbb1, ..., 2bbbb9, 2bbbcb, 2bbbcc, ... 一旦达到所有以开头的组合2,第一个字符串值将以 开头3

以下是我们拥有的算法,它工作正常,但有一些问题:

package com.test;

import java.util.LinkedList;

public class SequenceGenerate {

public static void main(String[] args) {

    //we can change the starting number 
    System.out.println("values " + buildListOfSequencesNewformate(1));

}

public static final long MAXIMUM_NUMERIC = 78125000;

public static final String ALPHA_NUMERIC = "bcdghjlmnpqrstvwz23456789";
public static final String NUMERIC = "23456789";


private static LinkedList<String> buildListOfSequencesNewformate(int i)
     {
LinkedList<String> lListOfSequences = new LinkedList<>();
long iSequenceDetails =32800;  // we can change this value 
while (i <= iSequenceDetails) {
    lListOfSequences.add(convertFromNumberToSequnece(i));
    i++;
}
return lListOfSequences;
}

private static String convertFromNumberToSequnece(long iOriginalSequence) {

    // Determine the number in base 8 value in order to extract the last two
    // digits of the Number
    String lStringOriginalSequenceBase8 = Long.toString(iOriginalSequence, 8);
    String lPaddedOriginalSequenceBase8 = padLeft(lStringOriginalSequenceBase8, 6, "0");

    //the first one digit
    String lFirstOneDigits = lPaddedOriginalSequenceBase8.substring(0, lPaddedOriginalSequenceBase8.length() - 5);

    //Extract the first 5 digits of the  Number
    // (in base8)
    String lFirstFiveDigitsBase8 = lPaddedOriginalSequenceBase8.substring(lPaddedOriginalSequenceBase8.length() - 5,
            lPaddedOriginalSequenceBase8.length());

    // convert the first five digits from base8 to base10
    long lFirstFiveDigitsBase10 = Long.parseLong(lFirstFiveDigitsBase8, 8);

    //Convert the first five digits from base10
    // to base25
    String lFirstFiveDigits = Long.toString(lFirstFiveDigitsBase10, 25);

    //Pad with zeros if required
    lFirstFiveDigits = padLeft(lFirstFiveDigits, 5, "0");

    char[] lB1UserID = new char[6];
    // Find the value of each character located at the position designated
    // by the value.
    lB1UserID[0] = NUMERIC.charAt(Integer.parseInt(lFirstOneDigits.substring(0, 1), 8));
    lB1UserID[1] = ALPHA_NUMERIC.charAt(Integer.parseInt(lFirstFiveDigits.substring(0, 1), 25));
    lB1UserID[2] = ALPHA_NUMERIC.charAt(Integer.parseInt(lFirstFiveDigits.substring(1, 2), 25));
    lB1UserID[3] = ALPHA_NUMERIC.charAt(Integer.parseInt(lFirstFiveDigits.substring(2, 3), 25));
    lB1UserID[4] = ALPHA_NUMERIC.charAt(Integer.parseInt(lFirstFiveDigits.substring(3, 4), 25));
    lB1UserID[5] = ALPHA_NUMERIC.charAt(Integer.parseInt(lFirstFiveDigits.substring(4, 5), 25));

    return new String(lB1UserID);
}

private static String padLeft(String anInputString, int iNbDigitToPad, String aStringToPad) {
    StringBuffer lStrRepresentation = new StringBuffer(anInputString);
    while (lStrRepresentation.length() < iNbDigitToPad) {
        lStrRepresentation.insert(0, aStringToPad);
    }

    return lStrRepresentation.toString();
}

}

该算法在 current_sequence 值之前工作正常,32767但问题从32768. 对于32767生成的当前序列2bddq2,这很好,但对于下一个序列,即32768,它正在生成3bbbbb’, which is incorrect. It should be2bddq3`。

任何解决问题的帮助将不胜感激。

标签: javaalgorithmsequence

解决方案


您可以简单地看到您的转换就像转换为另一个基数(前导零始终获得 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++;
    }

推荐阅读