为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

MM们 2022-05-21 13:33:38
我想生成字母数字递增序列,其中第一个字符串值应该是数字 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());}该算法在 current_sequence 值之前工作正常,32767但问题从32768. 对于32767生成的当前序列2bddq2,这很好,但对于下一个序列,即32768,它正在生成3bbbbb’, which is incorrect. It should be2bddq3`。任何解决问题的帮助将不胜感激。
查看完整描述

2 回答

?
慕桂英546537

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++;

    }


查看完整回答
反对 回复 2022-05-21
?
白板的微信

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);


然后您还可以将索引映射到您想要的任何数字方案。


查看完整回答
反对 回复 2022-05-21
  • 2 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号