随笔 - 0  文章 - 0  trackbacks - 0
<2026年11月>
日一二三四五六
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

留言簿(1)

文章分类

文章档案

搜索

  •  

最新评论

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 阅读(339) 评论(0)  编辑  收藏 所属分类: 算法

只有注册用户登录后才能发表评论。


网站导航: