Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases.
Show Tags
Have you met this question in a real interview? Yes No
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases.
Show Tags
Have you met this question in a real interview? Yes No
Discuss
这道题目和前面一题相对应这里依然可以当成26进制来做。
代码如下
def convert(num):
result=''
while num>0:
rest=(num-1)%26
result=chr(rest+65)+result
num=(num-1)/26
return result
def main():
num=27
#print chr(64)
print convert(num)
if __name__=="__main__":
main()
本文介绍了一种将正整数转换为Excel工作表中对应列标题的方法,例如1对应A,26对应Z,27对应AA等。该算法通过循环计算实现了从十进制到26进制的转换。
854

被折叠的 条评论
为什么被折叠?



