十进制转X
整数部分,对X短除取余倒序
小数部分,与X相乘取整正序
题目描述:
给定一个十进制数M,以及需要转换的进制数N。将十进制数M转化为N进制数
Python实现:
####输入两个整数##################
a = raw_input().strip().split(' ')
m = int(a[0])
n = int(a[1])
index = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
if m > 0:
temp = m
res = ''
while temp > 0:
a = temp%n
temp = temp/n
res = index[a] + res
else:
temp = -m
res = ''
while temp > 0:
a = temp%n
temp = temp/n
res = index[a] + res
res = '-'+res
print res