private static final char[] nchars = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z' };
public static String getNextString(String source, int step) {
if (source == null || source.trim().length() == 0)
return "0";
char[] temp = source.trim().toCharArray();
int capacity = nchars.length;
int carry = step;
for (int i = temp.length - 1; i >= 0; i--) {
char c = temp[i];
// get index of current char
int index = 0;
for (int n = 0; n < nchars.length; n++)
if (c == nchars[n]) {
index = n;
break;
}
// calc carry and mod
int total = index + carry;
index = total % capacity;
carry = total / capacity;
// if the mod value is nagative, borrow 1 from left
if (index < 0) {
index = capacity + index;
carry--;
}
temp[i] = nchars[index];
// out of range
if (carry != 0 && i == 0)
throw new RuntimeException(String.format(
"The number is out of range : %1$s + (%2$d)", source,
step));
if (carry == 0)
break;
}
return new String(temp);
}
posted on 2009-04-08 22:28
Rick Murphy 阅读(302)
评论(0) 编辑 收藏 所属分类:
算法