1 回答

TA貢獻(xiàn)1936條經(jīng)驗 獲得超7個贊
由于懶惰,我保留了您從 base10 轉(zhuǎn)換為 base7 的代碼,并添加了一些內(nèi)容以轉(zhuǎn)換為 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))
結(jié)果看起來像
$ python x.py
1331
512
4022
當(dāng)然有十億種方法可以更好地做到這一點,但它確實有效
添加回答
舉報