如何将数字从第 3 个数字系统转移到第 10 个,我这里有一个从第 10 个到第 7 个的例子:def fromtenthtoseven(x): result=" " while(x>0): result+=str(x%7) x=x//7 return(int(result[::-1]))print(fromtenthtoseven(512))第 10 个系统是512,第 7 个系统是1331
1 回答

LEATH
TA贡献1936条经验 获得超6个赞
由于懒惰,我保留了您从 base10 转换为 base7 的代码,并添加了一些内容以转换为 base10。
def fromtenthtoseven(x, frombase, tobase):
result=" "
if (frombase != 10):
sx = str(x)
p = 1
b = 0
while (sx != ""):
d = int(sx[-1])
sx = sx[:-1]
b = b + d * p
p = p * frombase
else:
b = x
while(b>0):
result+=str(b%tobase)
b=b//tobase
return(int(result[::-1]))
print(fromtenthtoseven(512, 10, 7))
print(fromtenthtoseven(1331, 7, 10))
print(fromtenthtoseven(1331, 7, 5))
结果看起来像
$ python x.py
1331
512
4022
当然有十亿种方法可以更好地做到这一点,但它确实有效
添加回答
举报
0/150
提交
取消