用一串字符串,有字母和数字,算出一列只有数字的序列号,有什么好的算法吗

解决方案 »

  1.   

    楼主问得这么含糊啊,用这么一个字符串算出只有数字的序列号,要简单也很简单,就是没什么用,把单个字符强制转换成int不就行了,顶多再取个模,得到介于0-9之间的数字而已
      

  2.   


    public class SerialNumber { public static long getSerialNum(String str){
    if (str == null){
    return 0;
    }
    byte[] bt = str.getBytes();
    int leng = bt.length;
    long sn = 0;
    for(int i = 0; i < leng; i++){
    sn += bt[i] * getPowerNum(leng - i - 1);
    }
    return sn;
    }

    private static long getPowerNum(int n){
    long num = 1;
    for(int i = 0; i < n; i++){
    num *= 10;
    }
    return num;
    }

    public static void main(String[] args){
    System.out.println(getSerialNum("qw12"));
    }
    }
      

  3.   

    楼主可以去看看密码学的知识再做是否可以考虑进行hash运算,得到一个串,取出数字部分,再把得到的串进行hash运算,再取出数字部分,直到数字的长度符合你要的为止,这只是一个想法
      

  4.   

    rower203(华仔) 的算法是可以,不过好像简单了点
    谁有做过这个的,共享一下吧