`
liugang594
  • 浏览: 978515 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

一个有趣的转换

阅读更多

下面代码实现了一个数字到字符系列的转换:

	public static String getChars(int number){
		if(number<=0){
			throw new IllegalArgumentException("The number should be a positive integer");
		}
		String result = "";
		while(number>0){
			number--;
			result =(char)(number%26+65)+result;
			number /= 26;
		}
		return result;
	}

 

 

转换规则如下:

 

1-->26: A-->Z

 

字符系列为:A...Z-->AA...AZ-->BA...BZ...ZZ-->AAA....

 

 

 例如:

            5 -> E

    26*26 -> YZ

26*26+1 -> ZA

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics