def calcusum(s1, s2): # 计算两个长整数(由字符串表示)之和,结果也返回字符串
size1, size2 = len(s1), len(s2)
index1, index2 = size1 - 1, size2 - 1
res = ""
tmp = 0
while index1 > -1 and index2 > -1:
tmp += int(s1[index1]) + int(s2[index2])
res = str(tmp%10) + res
tmp = tmp // 10
index1 -= 1
index2 -= 1
while index1 == -1 and index2 > -1:
tmp += int(s2[index2])
res = str(tmp%10) + res
tmp = tmp // 10
index2 -= 1
while index1 > -1 and index2 == -1:
tmp += int(s1[index1])
res = str(tmp%10) + res
tmp = tmp // 10
index1 -= 1
if tmp:
res = str(tmp % 10) + res
return res
Python 计算两个长整数(由字符串表示)之和,结果也返回字符串
最新推荐文章于 2023-11-24 18:59:38 发布