展开全部
def plus(x,y,result,a,index,carry):
index_abs = abs(index)
if index_abs <= min(len(x),len(y)):
xbit = x[index]
ybit = y[index]
elif index_abs <= len(x) and index_abs > len(y):
xbit = x[index]
ybit = 0
elif index_abs <= len(y) and index_abs > len(x):
xbit = 0
ybit = y[index]
else:
if carry != 0:
result.insert(0,carry)
return
bit = (xbit+ybit+carry)%a
carry = (xbit+ybit+carry)//a
print(carry,bit)
result.insert(0,bit)
plus(x,y,result,a,index-1,carry)
if __name__ == "__main__":
a = 16
x = [10,9,9]
y = [9,9]
result = []
plus(x,y,result,a,-1,0)
print(result)
运行2113结果5261如图4102:1653