流水號設計,要求數據庫裏存int,界面顯示的時候,顯示爲33進制(26個字母+10個數字,去掉容易混淆的字母I, L, O)
其實要求就是int與33進制互相轉換.java
具體實現以下:數據庫
package com.liu.algorithm; import java.util.Arrays; public class Notation { public static void main(String[] args) { String param = transform(1234567890,33); System.out.println("轉換的進制的數爲:"+ param); System.out.println("原始數爲:"+recover(param,33)); } // 係數集合 public static char[] coefficients = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G', 'H','J','K','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'}; /** * 進制轉換 * @param origin 須要轉換的原始數值 * @param notations 基數(進制) * @return 轉換後的進制數 */ private static String transform(int origin,int notations) { StringBuilder sb = new StringBuilder(); String result; while(origin != 0){ sb = sb.append(coefficients[origin%notations]); origin = origin/notations; } result = sb.reverse().toString(); return result; } /** * 獲取原始數值 * @param origin 轉換後的進制數 * @param notations 基數(進制) * @return 原始數值 */ private static int recover(String origin,int notations) { StringBuilder sb = new StringBuilder(origin); String string = sb.reverse().toString(); char[] chars = string.toCharArray(); int result = 0; int scale = 1; for (char cha : chars) { int coefficient = Arrays.binarySearch(coefficients,cha); result += coefficient*scale; scale *= notations; } return result; } }