我是靠谱客的博主 明理灯泡,这篇文章主要介绍Python 计算两个长整数(由字符串表示)之和,结果也返回字符串,现在分享给大家,希望可以做个参考。

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 计算两个长整数(由字符串表示)之和,结果也返回字符串的全部内容,更多相关Python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部