n进制.....???!!!
【摘要】 code 如下:"""divmod 实现 N∈[2,10],N进制转换"""import mathdef int_2_str_by_divmod(input_int, base=10, over_9_char_lib="ABCDEFGHIJKLMNOPQRSTUVWXYZ"): tran_cap = 10 + len(over_9_char_lib) if base > tran...
code 如下:
"""
divmod 实现 N∈[2,?],N进制转换
"""
import math
def int_2_str_by_divmod(input_int, base=10, over_9_char_lib="ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
tran_cap = 10 + len(over_9_char_lib)
if base > tran_cap or base < 2:
raise Exception("sorry,转换使用的进制不被支持")
ires = ""
set_neg = False
if input_int < 0:
input_int = -input_int
set_neg = True
# i, j = divmod(x, base)
i, j = (input_int // base, input_int % base)
if j < 10:
ires += str(j)
else:
ires += over_9_char_lib[j-10]
while i != 0:
# i, j = divmod(i, base)
i, j = (i // base, i % base)
ires += str(j)
if set_neg:
ires += "-"
return ires[::-1]
if __name__ == '__main__':
while True:
try:
x = int(input("输入一个整数:"))
y = int(input("输入转换进制:"))
z = input("超过十进制以后每一位表示数字的符号(不填认为是A-Z,支持到36进制):")
if len(z.strip()) == 0:
res = int_2_str_by_divmod(x, y)
else:
res = int_2_str_by_divmod(x, y, z)
print(type(res), res)
except Exception as e:
print(e)
各种符号组成的奇怪数字会不会冒出来 (^_^)
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)